-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy patherrors.ts
More file actions
99 lines (82 loc) · 2.63 KB
/
errors.ts
File metadata and controls
99 lines (82 loc) · 2.63 KB
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
91
92
93
94
95
96
97
98
99
import { GraphqlResponseError } from '@octokit/graphql';
import { RequestError } from '@octokit/request-error';
import { EVENTS } from '../../../shared/events';
import type { GitifyError } from '../../types';
import { Errors } from '../errors';
import { rendererLogError } from '../logger';
/**
* Determine the failure type based on an error.
* Handles Octokit RequestError (REST), GraphqlResponseError (GraphQL), and generic Error.
*
* @param err The error (RequestError, GraphqlResponseError, or generic Error)
* @returns The Gitify error type
*/
export function determineFailureType(
err: Error | RequestError | GraphqlResponseError<unknown>,
): GitifyError {
const message = err.message || '';
// Check for safe storage decryption failures first (happens before API call)
if (message.includes(EVENTS.SAFE_STORAGE_DECRYPT)) {
return Errors.BAD_CREDENTIALS;
}
// Handle Octokit REST RequestError
if (err instanceof RequestError) {
const status = err.status;
switch (status) {
case 401:
return Errors.BAD_CREDENTIALS;
case 403:
if (message.includes("Missing the 'notifications' scope")) {
return Errors.MISSING_SCOPES;
}
if (
message.includes('API rate limit exceeded') ||
message.includes('You have exceeded a secondary rate limit')
) {
return Errors.RATE_LIMITED;
}
break;
case 500:
return Errors.NETWORK;
default:
break;
}
}
// Handle Octokit GraphQL GraphqlResponseError
if (err instanceof GraphqlResponseError) {
const errorMessages =
err.errors?.map((e) => e.message).join('; ') || message;
if (errorMessages.includes('Bad credentials')) {
return Errors.BAD_CREDENTIALS;
}
if (
errorMessages.includes('API rate limit exceeded') ||
errorMessages.includes('You have exceeded a secondary rate limit')
) {
return Errors.RATE_LIMITED;
}
}
return Errors.UNKNOWN;
}
/**
* Handle GraphQL response errors.
* Logs and throws if `errors` array is present and non-empty.
*
* @param context The context of the GraphQL request
* @param payload The GraphQL response payload
*/
export function handleGraphQLResponseError<TResult>(
context: string,
payload: GraphqlResponseError<TResult>,
): never {
const errorMessages = payload.errors
.map((graphQLError) => graphQLError.message)
.join('; ');
const err = new Error(
errorMessages
? `GraphQL request returned errors: ${errorMessages}`
: 'GraphQL request returned errors',
);
rendererLogError(context, 'GraphQL errors present in response', err);
throw err;
}