Skip to content

Commit

Permalink
Have sanitisedError always return an Error instance
Browse files Browse the repository at this point in the history
…for the same reasons as explained in ministryofjustice/hmpps-template-typescript#197
  • Loading branch information
ushkarev committed Jun 12, 2023
1 parent ae167a1 commit 6c41a4e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
10 changes: 10 additions & 0 deletions server/routes/incentiveLevels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ describe('Incentive level management', () => {

it('should 404 if level does not exist', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 404,
message: 'Not Found',
stack: 'Not Found',
Expand Down Expand Up @@ -391,6 +392,7 @@ describe('Incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 500,
message: 'Internal Server Error',
stack: 'Internal Server Error',
Expand Down Expand Up @@ -419,6 +421,7 @@ describe('Incentive level management', () => {
const $ = jquery(new JSDOM().window) as unknown as typeof jquery

const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down Expand Up @@ -460,6 +463,7 @@ describe('Incentive level management', () => {

it('should 404 if level does not exist', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 404,
message: 'Not Found',
stack: 'Not Found',
Expand Down Expand Up @@ -661,6 +665,7 @@ describe('Incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 500,
message: 'Internal Server Error',
stack: 'Internal Server Error',
Expand Down Expand Up @@ -690,6 +695,7 @@ describe('Incentive level management', () => {
const $ = jquery(new JSDOM().window) as unknown as typeof jquery

const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down Expand Up @@ -732,6 +738,7 @@ describe('Incentive level management', () => {

it('should 404 if level does not exist', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 404,
message: 'Not Found',
stack: 'Not Found',
Expand Down Expand Up @@ -829,6 +836,7 @@ describe('Incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 500,
message: 'Internal Server Error',
stack: 'Internal Server Error',
Expand Down Expand Up @@ -858,6 +866,7 @@ describe('Incentive level management', () => {
const $ = jquery(new JSDOM().window) as unknown as typeof jquery

const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down Expand Up @@ -1060,6 +1069,7 @@ describe('Incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Internal Server Error',
stack: 'Internal Server Error',
Expand Down
4 changes: 4 additions & 0 deletions server/routes/prisonIncentiveLevels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ describe('Prison incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand All @@ -536,6 +537,7 @@ describe('Prison incentive level management', () => {

it('should show specific error message if there are prisoners on the level', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down Expand Up @@ -751,6 +753,7 @@ describe('Prison incentive level management', () => {

it('should show error message returned by api', () => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down Expand Up @@ -1174,6 +1177,7 @@ describe('Prison incentive level management', () => {
},
])('should show error message returned by api ($scenario)', ({ url }) => {
const error: SanitisedError<ErrorResponse> = {
name: 'Error',
status: 400,
message: 'Bad Request',
stack: 'Error: Bad Request',
Expand Down
13 changes: 8 additions & 5 deletions server/sanitisedError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ describe('sanitised error', () => {
const error = {
message: 'error description',
} as unknown as UnsanitisedError
expect(sanitisedError(error)).toEqual({
message: 'error description',
})

const expectedError = new Error('error description')

expect(sanitisedError(error)).toEqual(expectedError)
})

it('it should return an empty object for an unknown error structure', () => {
it('it should return an empty error for an unknown error structure', () => {
const error = {
property: 'unknown',
} as unknown as UnsanitisedError
expect(sanitisedError(error)).toEqual({})

expect(sanitisedError(error)).toBeInstanceOf(Error)
expect(sanitisedError(error)).not.toHaveProperty('property')
})
})
14 changes: 5 additions & 9 deletions server/sanitisedError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ResponseError } from 'superagent'

export interface SanitisedError<Data = unknown> {
export interface SanitisedError<Data = unknown> extends Error {
text?: string
status?: number
headers?: unknown
Expand All @@ -12,18 +12,14 @@ export interface SanitisedError<Data = unknown> {
export type UnsanitisedError = ResponseError

export default function sanitise<Data = unknown>(error: UnsanitisedError): SanitisedError<Data> {
const e = new Error() as SanitisedError<Data>
e.message = error.message
e.stack = error.stack
if (error.response) {
const e = new Error(error.message) as SanitisedError<Data>
e.text = error.response.text
e.status = error.response.status
e.headers = error.response.headers
e.data = error.response.body
e.message = error.message
e.stack = error.stack
return e
}
return {
message: error.message,
stack: error.stack,
}
return e
}

0 comments on commit 6c41a4e

Please sign in to comment.