diff --git a/__tests__/servers/web/web.js b/__tests__/servers/web/web.js index 18d4b52a7..821eb8d94 100644 --- a/__tests__/servers/web/web.js +++ b/__tests__/servers/web/web.js @@ -710,6 +710,11 @@ describe('Server: Web', () => { const validRandomKeys = ['key1', 'key2', 'key3'] const validRandomKey = validRandomKeys.indexOf(data.params.randomKey) > -1 if (!validRandomKey) { + if (data.params.randomKey === 'expired-key') { + const expiredError = new Error(`999: Key '${data.params.randomKey}' is expired`) + expiredError.code = 999 + throw expiredError + } const suspiciousError = new Error(`402: Suspicious Activity detected with key ${data.params.randomKey}`) suspiciousError.code = 402 throw suspiciousError @@ -803,6 +808,18 @@ describe('Server: Web', () => { const receivedBody = await toJson(responseWithKeyAndQuery.body) expect(receivedBody.good).toEqual(true) }) + + test('should not work for 999 status code set using custom error and default error code, 400 is thrown', async () => { + try { + await request.post(url + '/api/statusTestAction', { form: { key: 'value', randomKey: 'expired-key' } }) + throw new Error('should not get here') + } catch (error) { + expect(error.statusCode).not.toEqual(999) + expect(error.statusCode).toEqual(400) + const body = await toJson(error.response.body) + expect(body.error).toEqual('999: Key \'expired-key\' is expired') + } + }) }) }) diff --git a/tutorials/web-server.md b/tutorials/web-server.md index b9ea175e3..aa83e7953 100644 --- a/tutorials/web-server.md +++ b/tutorials/web-server.md @@ -151,8 +151,9 @@ exports['default'] = { }, // When true, returnErrorCodes will modify the response header for http(s) clients if connection.error is not null. // You can also set connection.rawConnection.responseHttpCode to specify a code per request. - /** - * To create custom Error objects with custom response code you need to add the your response status code in the error object eg: + /** + * To create custom Error objects with custom response code you need to add the your response status code in the error object. + * // Note: Error code in error object will overwrite the response's status code. * @example * * // Your action @@ -173,6 +174,7 @@ exports['default'] = { * } * ... * } + * // Only the values between 100 and 599 are accepted for status code, otherwise, it will be ignored. * */ returnErrorCodes: true, // should this node server attempt to gzip responses if the client can accept them?