Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add variable validation to SaveMediaListEntry.ts and UpdateMediaListE…
Browse files Browse the repository at this point in the history
…ntries.ts Mutations
  • Loading branch information
RLAlpha49 committed Apr 30, 2024
1 parent 00baefc commit a7e8fcd
Show file tree
Hide file tree
Showing 341 changed files with 2,217 additions and 2,009 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,83 @@ List of methods in `anilist.mutation`:
- saveMediaListEntry
- updateMediaListEntries

## Error Handling

AniLink will throw an error if the AniList API returns an error. You can catch these errors using a try-catch block.

```typescript
try {
const user = await aniLink.anilist.query.user({id: 542244});
console.log(user);
} catch (error) {
console.error(error);
}
```

This includes status codes and error messages returned by the AniList API. Here is an example rate limit handler to catch the errors thrown by AniLink:

### Typescript

```typescript
async function handleRateLimit(apiCall: () => Promise<any>, retryAfter = 60) {
try {
let response;
try {
response = await apiCall();
} catch (error) {
throw error;
}
console.log(response.data);
return response;
} catch (error: any) {
if (error.response && error.response.status === 429) {
console.log('Rate limit exceeded, waiting for 1 minute before retrying...');
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
console.log('Retrying...');
return handleRateLimit(apiCall, retryAfter);
} else {
if (error.response && error.response.data) {
throw error.response.data;
} else {
throw error.response || error;
}
}
}
}
```

### Javascript

```javascript
async function handleRateLimit(apiCall, retryAfter = 60) {
// Same as above
}
```

The possible error codes returned by the AniList API are:
- 400: Bad Request (e.g. missing variables, invalid variables, or invalid query)
- 401: Unauthorized (e.g. invalid authentication token)
- 404: Not Found (e.g. user not found)
- 429: Too Many Requests (e.g. rate limit exceeded)
- 500: Internal Server Error (e.g. AniList server error)

### Missing or Invalid Variables

AniLink will also throw an error if any variables are missing or invalid. For example, if you try to query a user providing a string instead of ID, AniLink will throw an error. Most variables are optional however there a few that are required.
```typescript
try {
const user = await aniLink.anilist.query.user({id: '542244'});
console.log(user);
} catch (error) {
console.error(error);
}
```

Example Error Thrown:

```typescript
Invalid id: 542244. Expected type: number
```

## Examples

Expand Down
4 changes: 2 additions & 2 deletions __tests__/anilist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ describe('Anilist API mutation', () => {
test('Save Media List Entries', async () => {
const variables = {
mediaId: 1,
status: 'CURRENT' as MediaListStatus,
status: 'CURRENT',
score: 8.5,
progress: 3,
};
Expand All @@ -414,7 +414,7 @@ describe('Anilist API mutation', () => {

test('Update Media List Entries', async () => {
const variables = {
status: 'CURRENT' as MediaListStatus,
status: 'CURRENT',
score: 8.5,
progress: 3,
ids: [143271, 156822, 170890],
Expand Down
22 changes: 18 additions & 4 deletions docs/assets/highlight.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
--dark-hl-5: #569CD6;
--light-hl-6: #0070C1;
--dark-hl-6: #4FC1FF;
--light-hl-7: #008000;
--dark-hl-7: #6A9955;
--light-hl-8: #098658;
--dark-hl-8: #B5CEA8;
--light-hl-7: #098658;
--dark-hl-7: #B5CEA8;
--light-hl-8: #267F99;
--dark-hl-8: #4EC9B0;
--light-hl-9: #008000;
--dark-hl-9: #6A9955;
--light-hl-10: #000000;
--dark-hl-10: #C8C8C8;
--light-code-background: #FFFFFF;
--dark-code-background: #1E1E1E;
}
Expand All @@ -31,6 +35,8 @@
--hl-6: var(--light-hl-6);
--hl-7: var(--light-hl-7);
--hl-8: var(--light-hl-8);
--hl-9: var(--light-hl-9);
--hl-10: var(--light-hl-10);
--code-background: var(--light-code-background);
} }

Expand All @@ -44,6 +50,8 @@
--hl-6: var(--dark-hl-6);
--hl-7: var(--dark-hl-7);
--hl-8: var(--dark-hl-8);
--hl-9: var(--dark-hl-9);
--hl-10: var(--dark-hl-10);
--code-background: var(--dark-code-background);
} }

Expand All @@ -57,6 +65,8 @@
--hl-6: var(--light-hl-6);
--hl-7: var(--light-hl-7);
--hl-8: var(--light-hl-8);
--hl-9: var(--light-hl-9);
--hl-10: var(--light-hl-10);
--code-background: var(--light-code-background);
}

Expand All @@ -70,6 +80,8 @@
--hl-6: var(--dark-hl-6);
--hl-7: var(--dark-hl-7);
--hl-8: var(--dark-hl-8);
--hl-9: var(--dark-hl-9);
--hl-10: var(--dark-hl-10);
--code-background: var(--dark-code-background);
}

Expand All @@ -82,4 +94,6 @@
.hl-6 { color: var(--hl-6); }
.hl-7 { color: var(--hl-7); }
.hl-8 { color: var(--hl-8); }
.hl-9 { color: var(--hl-9); }
.hl-10 { color: var(--hl-10); }
pre, code { background: var(--code-background); }
2 changes: 1 addition & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit a7e8fcd

Please sign in to comment.