Skip to content

Commit

Permalink
Improve error messages and format
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Dec 25, 2023
1 parent 8385eee commit cfe294c
Show file tree
Hide file tree
Showing 5 changed files with 1,045 additions and 1,074 deletions.
31 changes: 31 additions & 0 deletions packages/requester-utils/src/GitbeakerError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable max-classes-per-file */
export class GitbeakerRequestError extends Error {
constructor(
message: string,
options?: {
cause: {
description: string;
request: Request;
response: Response;
};
},
) {
super(message, options);

this.name = 'GitbeakerRequestError';
}
}

export class GitbeakerTimeoutError extends Error {
constructor(message: string) {
super(message);
this.name = 'GitbeakerTimeoutError';
}
}

export class GitbeakerRetryError extends Error {
constructor(message: string) {
super(message);
this.name = 'GitbeakerTimeoutError';
}
}
1 change: 1 addition & 0 deletions packages/requester-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './RequesterUtils';
export * from './BaseResource';
export * from './GitbeakerError';
33 changes: 11 additions & 22 deletions packages/rest/src/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ import type {
ResourceOptions,
ResponseBodyTypes,
} from '@gitbeaker/requester-utils';
import { createRequesterFn, getMatchingRateLimiter } from '@gitbeaker/requester-utils';

class GitbeakerError extends Error {
constructor(
message: string,
options?: {
cause: {
description: string;
request: Request;
response: Response;
};
},
) {
super(message, options);

this.name = 'GitbeakerError';
}
}
import {
GitbeakerRequestError,
GitbeakerRetryError,
GitbeakerTimeoutError,
createRequesterFn,
getMatchingRateLimiter,
} from '@gitbeaker/requester-utils';

export async function defaultOptionsHandler(
resourceOptions: ResourceOptions,
Expand Down Expand Up @@ -83,7 +72,7 @@ async function parseResponse(response: Response, asStream = false) {
async function throwFailedRequestError(
request: Request,
response: Response,
): Promise<GitbeakerAPIError> {
): Promise<GitbeakerRequestError> {
const content = await response.text();
const contentType = response.headers.get('Content-Type');
let description = 'API Request Error';
Expand All @@ -96,7 +85,7 @@ async function throwFailedRequestError(
description = content;
}

throw new GitbeakerError(response.statusText, {
throw new GitbeakerRequestError(response.statusText, {
cause: {
description,
request,
Expand Down Expand Up @@ -134,7 +123,7 @@ export async function defaultRequestHandler(endpoint: string, options?: RequestO

const response = await fetch(request).catch((e) => {
if (e.name === 'TimeoutError' || e.name === 'AbortError') {
throw new Error('Query timeout was reached');
throw new GitbeakerTimeoutError('Query timeout was reached');
}

throw e;
Expand All @@ -151,7 +140,7 @@ export async function defaultRequestHandler(endpoint: string, options?: RequestO
}
/* eslint-enable */

throw new Error(
throw new GitbeakerRetryError(
`Could not successfully complete this request due to Error 429. Check the applicable rate limits for this endpoint.`,
);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/rest/test/unit/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('defaultRequestHandler', () => {

await expect(defaultRequestHandler('http://test.com', {} as RequestOptions)).rejects.toThrow({
message: 'Really Bad Error',
name: 'Error',
name: 'GitbeakerRequestError',
cause: {
description: 'msg',
},
Expand All @@ -141,7 +141,7 @@ describe('defaultRequestHandler', () => {

await expect(defaultRequestHandler('http://test.com', {} as RequestOptions)).rejects.toThrow({
message: 'Really Bad Error',
name: 'Error',
name: 'GitbeakerRequestError',
cause: {
description: stringBody,
},
Expand All @@ -160,7 +160,7 @@ describe('defaultRequestHandler', () => {

await expect(defaultRequestHandler('http://test.com', {} as RequestOptions)).rejects.toThrow({
message: 'Query timeout was reached',
name: 'Error',
name: 'GitbeakerTimeoutError',
});
});

Expand All @@ -176,7 +176,7 @@ describe('defaultRequestHandler', () => {

await expect(defaultRequestHandler('http://test.com', {} as RequestOptions)).rejects.toThrow({
message: 'Query timeout was reached',
name: 'Error',
name: 'GitbeakerTimeoutError',
});
});

Expand Down Expand Up @@ -287,7 +287,7 @@ describe('defaultRequestHandler', () => {
await expect(defaultRequestHandler('http://test.com', {} as RequestOptions)).rejects.toThrow({
message:
'Could not successfully complete this request due to Error 429. Check the applicable rate limits for this endpoint.',
name: 'Error',
name: 'GitbeakerRetryError',
});

MockFetch.mockRestore();
Expand Down
Loading

0 comments on commit cfe294c

Please sign in to comment.