From bfe40a59d13e5a092736ef5554d588095aa1dcb4 Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Mon, 22 Jan 2024 19:52:20 -0500 Subject: [PATCH] Updating Gitbeaker Error typings (#3513) --- .gitlab-ci.yml | 14 ++-- .../requester-utils/src/GitbeakerError.ts | 41 ++++++++++-- .../test/unit/GitbeakerError.ts | 66 +++++++++++++++++++ packages/rest/src/Requester.ts | 2 +- 4 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 packages/requester-utils/test/unit/GitbeakerError.ts diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cf481022c..e0af2e02d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 @@ -57,7 +57,7 @@ install: build: stage: build - image: node:18-alpine + image: node:20-alpine needs: - install cache: @@ -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: @@ -98,7 +98,7 @@ lint: format: stage: format - image: node:18-alpine + image: node:20-alpine needs: - install cache: @@ -113,7 +113,7 @@ format: # Tests .test:base: stage: test - image: node:18-alpine + image: node:20-alpine needs: - install cache: @@ -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 @@ -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 diff --git a/packages/requester-utils/src/GitbeakerError.ts b/packages/requester-utils/src/GitbeakerError.ts index 3af478d16..e5b2739a3 100644 --- a/packages/requester-utils/src/GitbeakerError.ts +++ b/packages/requester-utils/src/GitbeakerError.ts @@ -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; @@ -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'; } } diff --git a/packages/requester-utils/test/unit/GitbeakerError.ts b/packages/requester-utils/test/unit/GitbeakerError.ts new file mode 100644 index 000000000..8966c517e --- /dev/null +++ b/packages/requester-utils/test/unit/GitbeakerError.ts @@ -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'); + }); +}); diff --git a/packages/rest/src/Requester.ts b/packages/rest/src/Requester.ts index 86052a34f..2d6f89a46 100644 --- a/packages/rest/src/Requester.ts +++ b/packages/rest/src/Requester.ts @@ -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; }