Skip to content

Commit

Permalink
feat(client): Mention Accelerate in "Too many connections" errors (#2…
Browse files Browse the repository at this point in the history
…3113)

* feat(client): Mention Accelerate in "Too many connections" errors

Close prisma/team-orm#945

* initial link

* Update packages/client/src/runtime/core/errors/utils/prismaGraphQLToJSError.ts

---------

Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
  • Loading branch information
SevInf and janpio committed Feb 20, 2024
1 parent 78c4be6 commit 95ce26f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Expand Up @@ -698,7 +698,7 @@ You very likely have the wrong "binaryTarget" defined in the schema.prisma file.

if (data.errors) {
if (data.errors.length === 1) {
throw prismaGraphQLToJSError(data.errors[0], this.clientVersion!)
throw prismaGraphQLToJSError(data.errors[0], this.clientVersion!, this.config.activeProvider!)
}
// this case should not happen, as the query engine only returns one error
throw new PrismaClientUnknownRequestError(JSON.stringify(data.errors), { clientVersion: this.clientVersion! })
Expand Down Expand Up @@ -760,15 +760,15 @@ You very likely have the wrong "binaryTarget" defined in the schema.prisma file.
if (Array.isArray(batchResult)) {
return batchResult.map((result) => {
if (result.errors && result.errors.length > 0) {
return prismaGraphQLToJSError(result.errors[0], this.clientVersion!)
return prismaGraphQLToJSError(result.errors[0], this.clientVersion!, this.config.activeProvider!)
}
return {
data: result,
elapsed,
}
})
} else {
throw prismaGraphQLToJSError(data.errors[0], this.clientVersion!)
throw prismaGraphQLToJSError(data.errors[0], this.clientVersion!, this.config.activeProvider!)
}
})
.catch(async (e) => {
Expand Down
Expand Up @@ -335,7 +335,7 @@ export class DataProxyEngine implements Engine<DataProxyTxInfoPayload> {

return batchResult.map((result) => {
if ('errors' in result && result.errors.length > 0) {
return prismaGraphQLToJSError(result.errors[0], this.clientVersion!)
return prismaGraphQLToJSError(result.errors[0], this.clientVersion!, this.config.activeProvider!)
}
return {
data: result as T,
Expand Down Expand Up @@ -389,7 +389,7 @@ export class DataProxyEngine implements Engine<DataProxyTxInfoPayload> {

if (json.errors) {
if (json.errors.length === 1) {
throw prismaGraphQLToJSError(json.errors[0], this.config.clientVersion!)
throw prismaGraphQLToJSError(json.errors[0], this.config.clientVersion!, this.config.activeProvider!)
} else {
throw new PrismaClientUnknownRequestError(json.errors, { clientVersion: this.config.clientVersion! })
}
Expand Down
Expand Up @@ -532,7 +532,9 @@ You may have to run ${green('prisma generate')} for your changes to take effect.

const externalError = this.getExternalAdapterError(error.user_facing_error)

return externalError ? externalError.error : prismaGraphQLToJSError(error, this.config.clientVersion!)
return externalError
? externalError.error
: prismaGraphQLToJSError(error, this.config.clientVersion!, this.config.activeProvider!)
}

private getExternalAdapterError(error: RequestError['user_facing_error']): ErrorRecord | undefined {
Expand Down
Expand Up @@ -2,12 +2,15 @@ import { RequestError } from '../../engines/common/types/RequestError'
import { PrismaClientKnownRequestError } from '../PrismaClientKnownRequestError'
import { PrismaClientUnknownRequestError } from '../PrismaClientUnknownRequestError'

const TOO_MANY_CONNECTIONS_ERROR = 'P2037'

export function prismaGraphQLToJSError(
{ error, user_facing_error }: RequestError,
clientVersion: string,
activeProvider: string,
): PrismaClientKnownRequestError | PrismaClientUnknownRequestError {
if (user_facing_error.error_code) {
return new PrismaClientKnownRequestError(user_facing_error.message, {
return new PrismaClientKnownRequestError(getKnownErrorMessage(user_facing_error, activeProvider), {
code: user_facing_error.error_code,
clientVersion,
meta: user_facing_error.meta,
Expand All @@ -20,3 +23,16 @@ export function prismaGraphQLToJSError(
batchRequestIdx: user_facing_error.batch_request_idx,
})
}

function getKnownErrorMessage(userFacingError: RequestError['user_facing_error'], activeProvider: string) {
let message = userFacingError.message
if (
(activeProvider === 'postgresql' || activeProvider === 'postgres' || activeProvider === 'mysql') &&
userFacingError.error_code === TOO_MANY_CONNECTIONS_ERROR
) {
message +=
'\nPrisma Accelerate has built-in connection pooling to prevent such errors: https://pris.ly/client/error-accelerate'
}

return message
}

0 comments on commit 95ce26f

Please sign in to comment.