@forge/kvs
1.5.1-next.01.6.0-next.1
out/utils/__test__/error-handling.test.js~
out/utils/__test__/error-handling.test.jsModified+81−10
Index: package/out/utils/__test__/error-handling.test.js
===================================================================
--- package/out/utils/__test__/error-handling.test.js
+++ package/out/utils/__test__/error-handling.test.js
@@ -1,7 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("../../errors");
+const version_1 = require("../../version");
const error_handling_1 = require("../error-handling");
describe('error-handling', () => {
it('isForgeError', () => {
expect((0, error_handling_1.isForgeError)({ code: 'code', message: 'message' })).toBe(true);
@@ -29,57 +30,65 @@
describe('Forge errors - ForgeKvsAPIError', () => {
const message = 'A test error has occurred';
const code = 'ERROR_CODE';
it('should return a ForgeKvsAPIError when response body is a Forge error', async () => {
- const mockResponse = new Response(JSON.stringify({ code, message }), {
+ const body = JSON.stringify({ code, message });
+ const mockResponse = new Response(body, {
status: 400,
statusText: 'Bad Request',
headers: { 'x-trace-id': traceId }
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
status: 400,
statusText: 'Bad Request',
- traceId
+ traceId,
+ responseBodyLength: body.length
}, { code, message }));
});
it('should include context if present in the Forge error', async () => {
const context = { key: 'value' };
- const mockResponse = new Response(JSON.stringify({ code, message, context }), {
+ const body = JSON.stringify({ code, message, context });
+ const mockResponse = new Response(body, {
status: 400,
statusText: 'Bad Request',
headers: { 'x-trace-id': traceId }
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
status: 400,
statusText: 'Bad Request',
- traceId
+ traceId,
+ responseBodyLength: body.length
}, { code, message, context }));
});
it('should include top level additional fields if present in the Forge error', async () => {
const extraFields = { extraValue: 'value', debug: true };
- const mockResponse = new Response(JSON.stringify({ code, message, ...extraFields }), {
+ const body = JSON.stringify({ code, message, ...extraFields });
+ const mockResponse = new Response(body, {
status: 400,
statusText: 'Bad Request',
headers: { 'x-trace-id': traceId }
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
status: 400,
statusText: 'Bad Request',
- traceId
+ traceId,
+ responseBodyLength: body.length
}, { code, message, context: extraFields }));
});
it('should merge context and additional top level fields if both present in the Forge error', async () => {
const context = { key: 'value' };
const extraFields = { extraValue: 'value', debug: true };
- const mockResponse = new Response(JSON.stringify({ code, message, context, ...extraFields }), {
+ const body = JSON.stringify({ code, message, context, ...extraFields });
+ const mockResponse = new Response(body, {
status: 400,
statusText: 'Bad Request',
headers: { 'x-trace-id': traceId }
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toThrowError(new errors_1.ForgeKvsAPIError({
status: 400,
statusText: 'Bad Request',
- traceId
+ traceId,
+ responseBodyLength: body.length
}, { code, message, context: { ...context, ...extraFields } }));
});
describe('Handle non forge errors', () => {
it('returns an UNKNOWN_ERROR when no response body', async () => {
@@ -90,9 +99,10 @@
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
status: 404,
statusText: 'Not Found',
- traceId
+ traceId,
+ responseBodyLength: 0
}, {
code: 'UNKNOWN_ERROR',
context: { responseText: '' },
message: 'Unexpected error in Forge KVS API'
@@ -107,15 +117,76 @@
});
await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
status: 500,
statusText: 'Internal Server Error',
- traceId
+ traceId,
+ responseBodyLength: body.length
}, {
code: 'UNKNOWN_ERROR',
context: { responseText: body },
message: 'Unexpected error in Forge KVS API'
}));
});
});
});
+ describe('with request context', () => {
+ const requestContext = {
+ httpMethod: 'POST',
+ httpPath: '/api/v1/get'
+ };
+ it('should include request context in ForgeKvsAPIError when provided', async () => {
+ const body = JSON.stringify({ code: 'ERROR', message: 'Test error' });
+ const mockResponse = new Response(body, {
+ status: 400,
+ statusText: 'Bad Request',
+ headers: { 'x-trace-id': traceId }
+ });
+ await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse, requestContext)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
+ status: 400,
+ statusText: 'Bad Request',
+ traceId,
+ httpMethod: 'POST',
+ httpPath: '/api/v1/get',
+ responseBodyLength: body.length
+ }, { code: 'ERROR', message: 'Test error' }));
+ });
+ it('should include request context in UNKNOWN_ERROR when provided', async () => {
+ const body = 'not valid json';
+ const mockResponse = new Response(body, {
+ status: 500,
+ statusText: 'Internal Server Error',
+ headers: { 'x-trace-id': traceId }
+ });
+ await expect(async () => await (0, error_handling_1.checkResponseError)(mockResponse, requestContext)).rejects.toMatchError(new errors_1.ForgeKvsAPIError({
+ status: 500,
+ statusText: 'Internal Server Error',
+ traceId,
+ httpMethod: 'POST',
+ httpPath: '/api/v1/get',
+ responseBodyLength: body.length
+ }, {
+ code: 'UNKNOWN_ERROR',
+ context: { responseText: body },
+ message: 'Unexpected error in Forge KVS API'
+ }));
+ });
+ });
});
+ describe('packageVersion', () => {
+ it('should auto-populate packageVersion on ForgeKvsError', () => {
+ const error = new errors_1.ForgeKvsError('Test error');
+ expect(error.packageVersion).toBeDefined();
+ expect(typeof error.packageVersion).toBe('string');
+ expect(error.packageVersion).toMatch(/^\d+\.\d+\.\d+/);
+ });
+ it('should auto-populate packageVersion on ForgeKvsAPIError', () => {
+ const error = new errors_1.ForgeKvsAPIError({ status: 500, statusText: 'Internal Server Error' }, { code: 'ERROR', message: 'Test error' });
+ expect(error.packageVersion).toBeDefined();
+ expect(typeof error.packageVersion).toBe('string');
+ expect(error.packageVersion).toMatch(/^\d+\.\d+\.\d+/);
+ });
+ it('should match the version in package.json', () => {
+ const error = new errors_1.ForgeKvsError('Test error');
+ expect(error.packageVersion).toBe(version_1.PACKAGE_VERSION);
+ });
+ });
});