npm package diff

Package: @forge/sql

Versions: 2.2.0-next.2 - 2.2.0-next.3

File: package/out/utils/__test__/error-handling.test.js

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
@@ -0,0 +1,201 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+const node_fetch_1 = require("node-fetch");
+const errors_1 = require("../../errors");
+const error_handling_1 = require("../error-handling");
+describe('errorFromResponse', () => {
+    const traceId = 'trace-id';
+    it('should do nothing if response is ok', async () => {
+        const mockResponse = new node_fetch_1.Response('OK', {
+            status: 200,
+            statusText: 'OK'
+        });
+        await expect((0, error_handling_1.checkResponseError)(mockResponse)).resolves.toBeUndefined();
+    });
+    describe('Forge errors - ForgeSQLAPIError', () => {
+        const message = 'A test error has occurred';
+        const code = 'ERROR_CODE';
+        const suggestion = 'Do something different';
+        it('should return a ForgeSQLAPIError when response body is a Forge error', async () => {
+            const mockResponse = new node_fetch_1.Response(JSON.stringify({ code, message }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId,
+                    code,
+                    message,
+                    serverMessage: message
+                });
+            }
+        });
+        it('should include custom message if provided', async () => {
+            const customMessage = 'A custom message';
+            const mockResponse = new node_fetch_1.Response(JSON.stringify({ code, message }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse, customMessage);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId,
+                    code,
+                    message: customMessage,
+                    serverMessage: message
+                });
+            }
+        });
+        it('should include context if present in the Forge error', async () => {
+            const context = { key: 'value' };
+            const mockResponse = new node_fetch_1.Response(JSON.stringify({ code, message, context }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    code,
+                    message,
+                    context,
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId
+                });
+            }
+        });
+        it('should include top level additional fields if present in the Forge error', async () => {
+            const extraFields = { extraValue: 'value', debug: true };
+            const mockResponse = new node_fetch_1.Response(JSON.stringify({ code, message, ...extraFields }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    code,
+                    message,
+                    context: extraFields,
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId
+                });
+            }
+        });
+        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 node_fetch_1.Response(JSON.stringify({ code, message, context, ...extraFields }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    code,
+                    message,
+                    context: { ...context, ...extraFields },
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId
+                });
+            }
+        });
+        it('should include suggestion if present in the Forge error', async () => {
+            const mockResponse = new node_fetch_1.Response(JSON.stringify({ code, message, suggestion }), {
+                status: 400,
+                statusText: 'Bad Request',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIError);
+                expect(error).toMatchObject({
+                    code,
+                    message,
+                    suggestion,
+                    status: 400,
+                    statusText: 'Bad Request',
+                    traceId
+                });
+            }
+        });
+    });
+    describe('Unknown or not forge errors - ForgeSQLAPIUnknownError', () => {
+        it('returns an ForgeSQLAPIUnknownError when no response body', async () => {
+            const mockResponse = new node_fetch_1.Response(undefined, {
+                status: 404,
+                statusText: 'Not Found',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIUnknownError);
+                expect(error).toMatchObject({
+                    code: 'UNKNOWN_ERROR',
+                    status: 404,
+                    statusText: 'Not Found',
+                    traceId
+                });
+            }
+        });
+        it("returns ForgeSQLAPIUnknownError if there is a JSON body that isn't a forge error", async () => {
+            const body = JSON.stringify({ not: 'a forge error' });
+            const mockResponse = new node_fetch_1.Response(body, {
+                status: 500,
+                statusText: 'Internal Server Error',
+                headers: { 'x-trace-id': traceId }
+            });
+            try {
+                await (0, error_handling_1.checkResponseError)(mockResponse);
+                throw new Error('Expected error to be thrown');
+            }
+            catch (error) {
+                expect(error).toBeInstanceOf(errors_1.ForgeSQLAPIUnknownError);
+                expect(error).toMatchObject({
+                    code: 'UNKNOWN_ERROR',
+                    status: 500,
+                    statusText: 'Internal Server Error',
+                    responseText: body,
+                    traceId
+                });
+            }
+        });
+    });
+});