diff --git a/specs/error.spec.ts b/specs/error.spec.ts index bb0fb34a..f0626a34 100644 --- a/specs/error.spec.ts +++ b/specs/error.spec.ts @@ -1,5 +1,5 @@ -import { CommerceLayerClient } from '../src' +import { CommerceLayerClient, ErrorObj } from '../src' import { ErrorType } from '../src/error' import { getClient } from '../test/common' @@ -40,4 +40,23 @@ describe('SDK:error suite', () => { } }) + + it('ErrorInterceptor.error', async () => { + + cl = await getClient({}) + + cl.addResponseInterceptor(undefined, (error: ErrorObj): ErrorObj => { + expect(error).toBeDefined() + expect(error.request).toBeDefined() + return error + }) + + try { + await cl.customers.create({ email: '' }) + } catch (error) { + expect(error.type).toBe(ErrorType.RESPONSE) + } + + }) + }) diff --git a/src/fetch.ts b/src/fetch.ts index 762539e1..58527f84 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -26,12 +26,14 @@ export class FetchError extends Error { readonly #errors?: any[] readonly #status: number readonly #statusText: string + readonly #request?: Partial - constructor(status: number, statusText: string, body?: any) { + constructor(status: number, statusText: string, body?: any, request?: FetchRequestOptions) { super(statusText) this.#status = status this.#statusText = statusText if (body) this.#errors = body.errors + if (request) this.#request = request } @@ -41,6 +43,8 @@ export class FetchError extends Error { get statusText(): string { return this.#statusText } + get request(): Partial | undefined { return this.#request } + } @@ -75,7 +79,7 @@ export const fetchURL = async (url: URL, requestOptions: FetchRequestOptions, cl : undefined if (!response.ok) { - let error = new FetchError(response.status, response.statusText, responseBody) + let error = new FetchError(response.status, response.statusText, responseBody, requestOptions) if (interceptors?.response?.onFailure) error = await interceptors.response.onFailure(error) if (error) throw error }