From 96cecc7c9d5d5ff78668ce2928a425c8ce634b07 Mon Sep 17 00:00:00 2001 From: Tommy Brunn Date: Fri, 28 Jan 2022 12:18:30 +0100 Subject: [PATCH] Handle client-side Mappersmith errors better When mappersmith encounters a client-side error, the response body can be just a string with the error message, rather than an object with a message property. Currently the error message would be unavailable to users of the schema registry. --- src/api/middleware/errorMiddleware.spec.ts | 12 ++++++++++++ src/api/middleware/errorMiddleware.ts | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api/middleware/errorMiddleware.spec.ts b/src/api/middleware/errorMiddleware.spec.ts index afc88a2..5d6459a 100644 --- a/src/api/middleware/errorMiddleware.spec.ts +++ b/src/api/middleware/errorMiddleware.spec.ts @@ -44,6 +44,18 @@ describe('ErrorMiddleware', () => { ).rejects.toHaveProperty('message', `${middlewareParams.clientId} - ${message}`) }) + it('raises an error with a message in case of client-side errors', async () => { + const message = 'error message' + const response = createResponse(message) + + await expect( + executedMiddleware.response(() => Promise.reject(response), undefined), + ).rejects.toHaveProperty( + 'message', + `${middlewareParams.clientId} - Error, status 500: ${message}`, + ) + }) + it('raise an error with a default message if the error payload is empty', async () => { const response = createResponse('') diff --git a/src/api/middleware/errorMiddleware.ts b/src/api/middleware/errorMiddleware.ts index 1d0e381..4c3ac59 100644 --- a/src/api/middleware/errorMiddleware.ts +++ b/src/api/middleware/errorMiddleware.ts @@ -12,7 +12,10 @@ class ResponseError extends Error { url: string constructor(clientName: string, response: ConfluenceResponse) { - super(`${clientName} - ${response.data().message || `Error, status ${response.status()}`}`) + super( + `${clientName} - ${response.data().message || + `Error, status ${response.status()}${response.data() ? `: ${response.data()}` : ''}`}`, + ) const request = response.request() this.name = this.constructor.name