diff --git a/src/templates/core/functions/catchErrorCodes.hbs b/src/templates/core/functions/catchErrorCodes.hbs index ae74a454..42f69d07 100644 --- a/src/templates/core/functions/catchErrorCodes.hbs +++ b/src/templates/core/functions/catchErrorCodes.hbs @@ -1,3 +1,5 @@ + + export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => { const errors: Record = { 400: 'Bad Request', @@ -16,16 +18,18 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): } if (!result.ok) { - const resultBody = (() => { - try { - return JSON.stringify(result.body, null, 2); - } catch (err) { - return undefined; - } - })(); - throw new ApiError( - result, - `Generic Error: status: ${result.status ?? 'unknown'}; status text: ${result.statusText}; body: ${resultBody}` - ); + const errorStatus = result.status ?? 'unknown'; + const errorStatusText = result.statusText ?? 'unknown'; + const errorBody = (() => { + try { + return JSON.stringify(result.body, null, 2); + } catch (e) { + return undefined; + } + })(); + + throw new ApiError(options, result, + `Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}` + ); } }; diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 86d5cd36..bdd0ef67 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -498,7 +498,19 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): } if (!result.ok) { - throw new ApiError(options, result, 'Generic Error'); + const errorStatus = result.status ?? 'unknown'; + const errorStatusText = result.statusText ?? 'unknown'; + const errorBody = (() => { + try { + return JSON.stringify(result.body, null, 2); + } catch (e) { + return undefined; + } + })(); + + throw new ApiError(options, result, + \`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\` + ); } }; @@ -3593,7 +3605,19 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): } if (!result.ok) { - throw new ApiError(options, result, 'Generic Error'); + const errorStatus = result.status ?? 'unknown'; + const errorStatusText = result.statusText ?? 'unknown'; + const errorBody = (() => { + try { + return JSON.stringify(result.body, null, 2); + } catch (e) { + return undefined; + } + })(); + + throw new ApiError(options, result, + \`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\` + ); } }; diff --git a/test/e2e/client.angular.spec.ts b/test/e2e/client.angular.spec.ts index ef8deee8..ec77b5ee 100644 --- a/test/e2e/client.angular.spec.ts +++ b/test/e2e/client.angular.spec.ts @@ -139,10 +139,11 @@ describe('client.angular', () => { }); }); }); + expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/client.axios.spec.ts b/test/e2e/client.axios.spec.ts index ed8952b6..c3b28013 100644 --- a/test/e2e/client.axios.spec.ts +++ b/test/e2e/client.axios.spec.ts @@ -136,7 +136,7 @@ describe('client.axios', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/client.babel.spec.ts b/test/e2e/client.babel.spec.ts index 7fe79d0b..9a2671df 100644 --- a/test/e2e/client.babel.spec.ts +++ b/test/e2e/client.babel.spec.ts @@ -161,7 +161,7 @@ describe('client.babel', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/client.fetch.spec.ts b/test/e2e/client.fetch.spec.ts index 3a184438..54fd9ef2 100644 --- a/test/e2e/client.fetch.spec.ts +++ b/test/e2e/client.fetch.spec.ts @@ -156,7 +156,7 @@ describe('client.fetch', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/client.node.spec.ts b/test/e2e/client.node.spec.ts index 9967cf01..55bcee73 100644 --- a/test/e2e/client.node.spec.ts +++ b/test/e2e/client.node.spec.ts @@ -136,7 +136,7 @@ describe('client.node', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/client.xhr.spec.ts b/test/e2e/client.xhr.spec.ts index 487fb014..0b8f3e0e 100644 --- a/test/e2e/client.xhr.spec.ts +++ b/test/e2e/client.xhr.spec.ts @@ -155,7 +155,7 @@ describe('client.xhr', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.angular.spec.ts b/test/e2e/v3.angular.spec.ts index 2b039fe7..8b2faca7 100644 --- a/test/e2e/v3.angular.spec.ts +++ b/test/e2e/v3.angular.spec.ts @@ -144,7 +144,7 @@ describe('v3.angular', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.axios.spec.ts b/test/e2e/v3.axios.spec.ts index b43b0a54..c6e44e6a 100644 --- a/test/e2e/v3.axios.spec.ts +++ b/test/e2e/v3.axios.spec.ts @@ -127,7 +127,7 @@ describe('v3.axios', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.babel.spec.ts b/test/e2e/v3.babel.spec.ts index 42463b4b..c747c7e3 100644 --- a/test/e2e/v3.babel.spec.ts +++ b/test/e2e/v3.babel.spec.ts @@ -153,7 +153,7 @@ describe('v3.babel', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.fetch.spec.ts b/test/e2e/v3.fetch.spec.ts index 2fb58d1e..e1e721c9 100644 --- a/test/e2e/v3.fetch.spec.ts +++ b/test/e2e/v3.fetch.spec.ts @@ -147,7 +147,7 @@ describe('v3.fetch', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.node.spec.ts b/test/e2e/v3.node.spec.ts index 5dd4ec34..2adf0259 100644 --- a/test/e2e/v3.node.spec.ts +++ b/test/e2e/v3.node.spec.ts @@ -127,7 +127,7 @@ describe('v3.node', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict', diff --git a/test/e2e/v3.xhr.spec.ts b/test/e2e/v3.xhr.spec.ts index a117027a..e2ad2537 100644 --- a/test/e2e/v3.xhr.spec.ts +++ b/test/e2e/v3.xhr.spec.ts @@ -146,7 +146,7 @@ describe('v3.xhr', () => { expect(error).toBe( JSON.stringify({ name: 'ApiError', - message: 'Generic Error', + message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}', url: 'http://localhost:3000/base/api/v1.0/error?status=409', status: 409, statusText: 'Conflict',