Skip to content

Commit

Permalink
Update readme and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fsvreddit committed Oct 15, 2024
1 parent aa47a31 commit 7aacb61
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# social-links-bot
# social-links-bot

A Devvit app that responds to user lists and returns their social links (if any).

Object type returned is an array of the following type:

```ts
interface UserSocialLinks {
username?: string;
error?: string;
errorDetail?: string;
socialLinks?: UserSocialLink[];
}
```

Input is in the form of a JSON array of string e.g. `["user1", "user2"]`. Usernames are not case sensitive.

If all is well, the error/errorDetail attributes are omitted.

If the input is badly formed (e.g. not JSON, not a string array, containing entries that are impossible usernames (too short/long), or contains duplicate items), `error` and `errorDetail` attributes are returned in a single array item.

Otherwise, an array of usernames with their social links (if any) is returned.

Shadowbanned, suspended and deleted users cannot be retrieved, in which case their username and an error of "SHADOWBANNED" is returned.
2 changes: 1 addition & 1 deletion devvit.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name: social-links-bot
version: 0.0.14
version: 0.0.17
46 changes: 23 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,33 @@ Devvit.addTrigger({
}

const userSocialLinks: UserSocialLinks[] = [];
let userList: string[] = [];
try {
userList = JSON.parse(event.comment.body) as string[];
} catch (error) {
userSocialLinks.push({
error: "INVALID_JSON",
errorDetail: error instanceof Error ? JSON.stringify(error.message) : undefined,
});
}

const ajv = new Ajv.default();
const validate = ajv.compile(schema);

if (!validate(userList)) {
userSocialLinks.push({
error: "JSON_INVALID_FORMAT",
errorDetail: ajv.errorsText(validate.errors),
});
} else {
for (const user of userList) {
userSocialLinks.push(await getSocialLinksForUser(user, context));
try {
const userList = JSON.parse(event.comment.body) as string[];

const ajv = new Ajv.default();
const validate = ajv.compile(schema);

if (!validate(userList)) {
userSocialLinks.push({
error: "JSON_INVALID_FORMAT",
errorDetail: ajv.errorsText(validate.errors),
});
} else {
for (const user of userList) {
userSocialLinks.push(await getSocialLinksForUser(user, context));
}
}
}

if (userSocialLinks.length === 0) {
if (userSocialLinks.length === 0) {
userSocialLinks.push({
error: "NO_USERS_PROVIDED",
});
}
} catch (error) {
userSocialLinks.push({
error: "NO_USERS_PROVIDED",
error: "INVALID_JSON",
errorDetail: error instanceof Error ? error.message : undefined,
});
}

Expand Down

0 comments on commit 7aacb61

Please sign in to comment.