Skip to content

Commit

Permalink
feat: add details to error message (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Apr 3, 2024
1 parent b560051 commit 7367241
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/headers.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const NF_ERROR = 'x-nf-error'
export const NF_REQUEST_ID = 'x-nf-request-id'
19 changes: 10 additions & 9 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MockFetch } from '../test/mock_fetch.js'
import { base64Encode, streamToString } from '../test/util.js'

import { MissingBlobsEnvironmentError } from './environment.js'
import { NF_REQUEST_ID } from './headers.js'
import { NF_ERROR, NF_REQUEST_ID } from './headers.js'
import { getDeployStore, getStore, setEnvironmentContext } from './main.js'
import { base64Decode } from './util.js'

Expand Down Expand Up @@ -135,7 +135,7 @@ describe('get', () => {
})

expect(async () => await blobs.get(key)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response (ID: ${mockRequestID})`,
`Netlify Blobs has generated an internal error (401 status code, ID: ${mockRequestID})`,
)
expect(mockStore.fulfilled).toBeTruthy()
})
Expand All @@ -161,7 +161,7 @@ describe('get', () => {
})

await expect(async () => await blobs.get(key)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (401 status code)`,
)

expect(mockStore.fulfilled).toBeTruthy()
Expand Down Expand Up @@ -271,9 +271,10 @@ describe('get', () => {
})

test('Throws when an edge URL returns a non-200 status code', async () => {
const errorDetails = 'Failed to decode token: Token expired'
const mockStore = new MockFetch().get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(null, { status: 401 }),
response: new Response(null, { headers: { [NF_ERROR]: errorDetails }, status: 401 }),
url: `${edgeURL}/${siteID}/site:production/${key}`,
})

Expand All @@ -287,7 +288,7 @@ describe('get', () => {
})

await expect(async () => await blobs.get(key)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (${errorDetails})`,
)

expect(mockStore.fulfilled).toBeTruthy()
Expand Down Expand Up @@ -818,7 +819,7 @@ describe('set', () => {
})

expect(async () => await blobs.set(key, 'value')).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (401 status code)`,
)
expect(mockStore.fulfilled).toBeTruthy()
})
Expand Down Expand Up @@ -946,7 +947,7 @@ describe('set', () => {
})

await expect(async () => await blobs.set(key, value)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (401 status code)`,
)

expect(mockStore.fulfilled).toBeTruthy()
Expand Down Expand Up @@ -1169,7 +1170,7 @@ describe('delete', () => {
})

await expect(async () => await blobs.delete(key)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (401 status code)`,
)
expect(mockStore.fulfilled).toBeTruthy()
})
Expand Down Expand Up @@ -1235,7 +1236,7 @@ describe('delete', () => {
})

await expect(async () => await blobs.delete(key)).rejects.toThrowError(
`Netlify Blobs has generated an internal error: 401 response`,
`Netlify Blobs has generated an internal error (401 status code)`,
)

expect(mockStore.fulfilled).toBeTruthy()
Expand Down
2 changes: 1 addition & 1 deletion src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ test('If a token is set, rejects any requests with an invalid `authorization` he
})

await expect(async () => await blobs.get('some-key')).rejects.toThrowError(
'Netlify Blobs has generated an internal error: 403 response',
'Netlify Blobs has generated an internal error (403 status code)',
)

await server.stop()
Expand Down
8 changes: 4 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { NF_REQUEST_ID } from './headers.ts'
import { NF_ERROR, NF_REQUEST_ID } from './headers.ts'

export class BlobsInternalError extends Error {
constructor(res: Response) {
let message = `Netlify Blobs has generated an internal error: ${res.status} response`
let details = res.headers.get(NF_ERROR) || `${res.status} status code`

if (res.headers.has(NF_REQUEST_ID)) {
message += ` (ID: ${res.headers.get(NF_REQUEST_ID)})`
details += `, ID: ${res.headers.get(NF_REQUEST_ID)}`
}

super(message)
super(`Netlify Blobs has generated an internal error (${details})`)

this.name = 'BlobsInternalError'
}
Expand Down

0 comments on commit 7367241

Please sign in to comment.