Skip to content

Commit

Permalink
feat: add request ids to all messages for DataProxyAPIErrors (#14738)
Browse files Browse the repository at this point in the history
Co-authored-by: pierre <pierreantoine.urvoy@gmail.com>
  • Loading branch information
mavilein and millsp committed Aug 11, 2022
1 parent aa4bc4c commit a1b1f3c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/engine-core/src/__tests__/errors.test.ts
Expand Up @@ -5,11 +5,14 @@ import { getErrorMessageWithLink } from '../common/errors/utils/getErrorMessageW
import { responseToError } from '../data-proxy/errors/utils/responseToError'
import type { RequestResponse } from '../data-proxy/utils/request'

const response = (body: Promise<any>, code?: number): RequestResponse => ({
const response = (body: Promise<any>, code?: number, requestId?: string): RequestResponse => ({
json: () => body,
url: '',
ok: false,
status: code || 400,
headers: {
'Prisma-Request-Id': requestId,
},
})

describe('responseToError', () => {
Expand Down Expand Up @@ -102,6 +105,21 @@ describe('responseToError', () => {
expect(error.code).toEqual('P1000')
}
})

test('The PDP request Id is added to error messages if the header is present in the response', async () => {
expect.assertions(1)

const errorJSON = {
EngineNotStarted: {
reason: 'SchemaMissing',
},
}

const error = await responseToError(response(Promise.resolve(errorJSON), 404, 'some-request-id'), '')
if (error) {
expect(error.message).toEqual('Schema needs to be uploaded (The request id was: some-request-id)')
}
})
})

describe('getErrorMessageWithLink', () => {
Expand Down
Expand Up @@ -13,5 +13,12 @@ export abstract class DataProxyAPIError extends DataProxyError {
super(message, info)

this.response = info.response

// add request id to response message if it is present in the response header
const requestId = this.response.headers?.['Prisma-Request-Id']
if (requestId) {
const messageSuffix = `(The request id was: ${requestId})`
this.message = this.message + ' ' + messageSuffix
}
}
}
5 changes: 4 additions & 1 deletion packages/engine-core/src/data-proxy/utils/request.ts
Expand Up @@ -8,8 +8,10 @@ import { getJSRuntimeName } from './getJSRuntimeName'

// our implementation handles less
export type RequestOptions = O.Patch<{ headers?: { [k: string]: string }; body?: string }, RequestInit>

type Headers = Record<string, string | string[] | undefined>
export type RequestResponse = O.Required<
O.Optional<O.Patch<{ text: () => string }, Response>>,
O.Optional<O.Patch<{ text: () => string; headers: Headers }, Response>>,
'text' | 'json' | 'url' | 'ok' | 'status'
>

Expand Down Expand Up @@ -78,6 +80,7 @@ function buildResponse(incomingData: Buffer[], response: IncomingMessage): Reque
ok: response.statusCode! >= 200 && response.statusCode! <= 299,
status: response.statusCode!,
url: response.url!,
headers: response.headers,
}
}

Expand Down

0 comments on commit a1b1f3c

Please sign in to comment.