Skip to content

Commit

Permalink
Updating Gitbeaker Error typings (#3513)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple authored Jan 23, 2024
1 parent 7eec49b commit bfe40a5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 14 deletions.
14 changes: 7 additions & 7 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ variables:

#Link and Install all required dependancies
install:
image: node:18-alpine
image: node:20-alpine
stage: install
before_script:
- apk add --no-cache libc6-compat jq
Expand All @@ -57,7 +57,7 @@ install:

build:
stage: build
image: node:18-alpine
image: node:20-alpine
needs:
- install
cache:
Expand All @@ -82,7 +82,7 @@ build:
# Lint all code, tests and supporting documentation (README, CHANGELOG etc)
lint:
stage: lint
image: node:18-alpine
image: node:20-alpine
needs:
- install
cache:
Expand All @@ -98,7 +98,7 @@ lint:

format:
stage: format
image: node:18-alpine
image: node:20-alpine
needs:
- install
cache:
Expand All @@ -113,7 +113,7 @@ format:
# Tests
.test:base:
stage: test
image: node:18-alpine
image: node:20-alpine
needs:
- install
cache:
Expand Down Expand Up @@ -338,7 +338,7 @@ test:e2e:core:

test:e2e:rest:
extends: .test:e2e:base
image: mcr.microsoft.com/playwright:v1.40.0-focal
image: mcr.microsoft.com/playwright:v1.40.0-focal
before_script:
# reinstall swc due to missing binding for ubuntu image
- yarn add @swc/core
Expand All @@ -351,7 +351,7 @@ test:e2e:cli:
# Releases
.release:base:
stage: release
image: node:18-alpine
image: node:20-alpine
variables:
GIT_STRATEGY: clone
GIT_DEPTH: 0
Expand Down
41 changes: 35 additions & 6 deletions packages/requester-utils/src/GitbeakerError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-disable max-classes-per-file */
export class GitbeakerRequestError extends Error {
readonly cause?: {
description: string;
request: Request;
response: Response;
};

constructor(
message: string,
options?: {
cause: {
cause?: {
description: string;
request: Request;
response: Response;
Expand All @@ -12,20 +18,43 @@ export class GitbeakerRequestError extends Error {
) {
super(message, options);

this.cause = options?.cause;
this.name = 'GitbeakerRequestError';
}
}

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

// this.name = 'GitbeakerRequestError';
// }

// cause?: {
// description: string;
// request: Request;
// response: Response;
// };
// }
export class GitbeakerTimeoutError extends Error {
constructor(message: string) {
super(message);
constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'GitbeakerTimeoutError';
}
}

export class GitbeakerRetryError extends Error {
constructor(message: string) {
super(message);
this.name = 'GitbeakerTimeoutError';
constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'GitbeakerRetryError';
}
}
66 changes: 66 additions & 0 deletions packages/requester-utils/test/unit/GitbeakerError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
GitbeakerRequestError,
GitbeakerRetryError,
GitbeakerTimeoutError,
} from '../../src/GitbeakerError';

describe('GitbeakerRequestError', () => {
it('should create a custom error with the name "GitbeakerRequestError"', () => {
const error = new GitbeakerRequestError('my message');

expect(error.name).toBe('GitbeakerRequestError');
expect(error).toBeInstanceOf(GitbeakerRequestError);
});

it('should accept a message and a cause option', () => {
const error = new GitbeakerRequestError('my message', {
cause: {
description: 'test',
request: new Request('http://test.url'),
response: new Response(),
},
});

expect(error.message).toBe('my message');
expect(error?.cause?.description).toBe('test');
expect(error.cause?.request).toBeInstanceOf(Request);
expect(error.cause?.response).toBeInstanceOf(Response);
});

it('should accept a message without a cause option', () => {
const error = new GitbeakerRequestError('my message');

expect(error.message).toBe('my message');
expect(error?.cause).toBeUndefined();
});
});

describe('GitbeakerRetryError', () => {
it('should create a custom error with the name "GitbeakerRetryError"', () => {
const error = new GitbeakerRetryError('my message');

expect(error.name).toBe('GitbeakerRetryError');
expect(error).toBeInstanceOf(GitbeakerRetryError);
});

it('should accept error options', () => {
const error = new GitbeakerRetryError('my message', { cause: 'reason' });

expect(error.cause).toBe('reason');
});
});

describe('GitbeakerTimeoutError', () => {
it('should create a custom error with the name "GitbeakerTimeoutError"', () => {
const error = new GitbeakerTimeoutError('my message');

expect(error.name).toBe('GitbeakerTimeoutError');
expect(error).toBeInstanceOf(GitbeakerTimeoutError);
});

it('should accept error options', () => {
const error = new GitbeakerTimeoutError('my message', { cause: 'reason' });

expect(error.cause).toBe('reason');
});
});
2 changes: 1 addition & 1 deletion packages/rest/src/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function throwFailedRequestError(
if (contentType?.includes('application/json')) {
const output = JSON.parse(content);

description = JSON.stringify(output.error || output.message, null, 2);
description = output.message;
} else {
description = content;
}
Expand Down

0 comments on commit bfe40a5

Please sign in to comment.