-
Notifications
You must be signed in to change notification settings - Fork 71
/
errors.ts
90 lines (80 loc) · 2.15 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { ApolloError, FetchResult, ServerError } from '@apollo/client';
import { CausedError, isObject } from '@lens-protocol/shared-kernel';
/**
* @internal
*/
export enum ApolloServerErrorCode {
BAD_REQUEST = 'BAD_REQUEST',
BAD_USER_INPUT = 'BAD_USER_INPUT',
FORBIDDEN = 'FORBIDDEN',
GRAPHQL_PARSE_FAILED = 'GRAPHQL_PARSE_FAILED',
GRAPHQL_VALIDATION_FAILED = 'GRAPHQL_VALIDATION_FAILED',
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
OPERATION_RESOLUTION_FAILURE = 'OPERATION_RESOLUTION_FAILURE',
PERSISTED_QUERY_NOT_FOUND = 'PERSISTED_QUERY_NOT_FOUND',
PERSISTED_QUERY_NOT_SUPPORTED = 'PERSISTED_QUERY_NOT_SUPPORTED',
UNAUTHENTICATED = 'UNAUTHENTICATED',
}
/**
* @internal
*/
export function isValidationApolloError(e: unknown): e is ApolloError {
if (e instanceof ApolloError) {
if (
e.graphQLErrors[0] &&
e.graphQLErrors[0].extensions &&
e.graphQLErrors[0].extensions.code === ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED
) {
return true;
}
}
return false;
}
/**
* @internal
*/
export function graphQLResultHasUnauthenticatedError<T>(result: FetchResult<T>) {
return (
result.errors &&
result.errors.some((e) => e.extensions?.code === ApolloServerErrorCode.UNAUTHENTICATED)
);
}
/**
* @internal
*/
export function isServerError(error: unknown): error is ServerError {
return (
isObject(error) &&
error instanceof Error &&
error.name === 'ServerError' &&
`statusCode` in error
);
}
/**
* @internal
*/
export function isUnauthorizedServerError(error: unknown): error is ServerError {
return isServerError(error) && error.statusCode === 401;
}
/**
* An unexpected error, usually from an API response.
*
* See the error message for more details.
*/
export class UnspecifiedError extends CausedError {
name = 'UnspecifiedError' as const;
constructor(cause: Error) {
super(cause.message, { cause });
}
}
/**
* A GraphQL validation error from the API.
*
* See the error message for more details.
*/
export class ValidationError extends CausedError {
name = 'ValidationError' as const;
constructor(cause: Error) {
super(cause.message, { cause });
}
}