Skip to content

Commit

Permalink
Allow passing array of codes to isHttpErrorCode
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 18, 2023
1 parent 7448b5c commit 961f32f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-laws-relax.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': minor
---

Allow passing array of codes to `isHttpErrorCode`
19 changes: 18 additions & 1 deletion apps/website/docs/api/utils/error_guards.md
Expand Up @@ -71,7 +71,11 @@ const httpError = sample({

### `isHttpErrorCode`

This function is a more specific version of `isHttpError`. It takes a number as an argument and returns a function that checks if the error is a `HttpError` with the given status code.
This function is a more specific version of `isHttpError`.

#### `isHttpErrorCode(statusCode: number)`

It takes a number as an argument and returns a function that checks if the error is a `HttpError` with the given status code.

```ts
import { isHttpErrorCode } from '@farfetched/core';
Expand All @@ -82,6 +86,19 @@ const notFound = sample({
});
```

#### `isHttpErrorCode(statusCodes: number[])` <Badge type="tip" text="since v0.9.0" />

It takes an array of numbers as an argument and returns a function that checks if the error is a `HttpError` with one of the given status codes.

```ts
import { isHttpErrorCode } from '@farfetched/core';

const notFoundOrForbidden = sample({
clock: query.finished.failure,
filter: isHttpErrorCode([404, 403]),
});
```

## `isNetworkError`

`NetworkError` is thrown when the query fails because of network problems.
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/errors/__test__/guards.test.ts
@@ -0,0 +1,50 @@
import { describe, test, expect } from 'vitest';
import { httpError } from '../create_error';

import { isHttpErrorCode } from '../guards';

describe('isHttpErrorCode', () => {
test('isHttpErrorCode supports array of codes', () => {
const check401or403 = isHttpErrorCode([401, 403]);

expect(
check401or403({
error: httpError({
status: 401,
statusText: 'Text',
response: {},
}),
})
).toBe(true);

expect(
check401or403({
error: httpError({
status: 403,
statusText: 'Text',
response: {},
}),
})
).toBe(true);

expect(
check401or403({
error: httpError({
status: 400,
statusText: 'Text',
response: {},
}),
})
).toBe(false);

expect(
check401or403({
error: httpError({
status: 404,
statusText: 'Text',
response: {},
}),
})
).toBe(false);
});
});
12 changes: 7 additions & 5 deletions packages/core/src/errors/guards.ts
@@ -1,9 +1,9 @@
import {
ABORT,
AbortError,
type AbortError,
HTTP,
HttpError,
InvalidDataError,
type HttpError,
type InvalidDataError,
INVALID_DATA,
NETWORK,
NetworkError,
Expand Down Expand Up @@ -41,15 +41,17 @@ export function isHttpError(args: WithError): args is WithError<HttpError> {
return args.error?.errorType === HTTP;
}

export function isHttpErrorCode<Code extends number>(code: Code) {
export function isHttpErrorCode<Code extends number>(code: Code | Code[]) {
return function isExactHttpError(
args: WithError
): args is WithError<HttpError<Code>> {
if (!isHttpError(args)) {
return false;
}

return args.error.status === code;
const codes = Array.isArray(code) ? code : [code];

return codes.includes(args.error.status as any);
};
}

Expand Down

0 comments on commit 961f32f

Please sign in to comment.