Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/http/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyError } from '@fastify/error'
import { ErrorCode, isRenderableError, StorageBackendError, StorageError } from '@internal/errors'
import { isDatabaseSlowDownError } from '@internal/errors/database-error'
import { FastifyInstance } from 'fastify'
import { DatabaseError } from 'pg'

/**
* The global error handler for all the uncaught exceptions within a request.
Expand All @@ -24,17 +24,7 @@ export const setErrorHandler = (
request.executionError = error

// database error
if (
error instanceof DatabaseError &&
[
'Authentication error', // supavisor specific
'Max client connections reached',
'remaining connection slots are reserved for non-replication superuser connections',
'no more connections allowed',
'sorry, too many clients already',
'server login has been failing, try again later',
].some((msg) => (error as DatabaseError).message.includes(msg))
) {
if (isDatabaseSlowDownError(error)) {
return reply.status(429).send(
formatter({
statusCode: `429`,
Expand Down
13 changes: 2 additions & 11 deletions src/http/routes/s3/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { S3ServiceException } from '@aws-sdk/client-s3'
import { FastifyError } from '@fastify/error'
import { ErrorCode, StorageBackendError } from '@internal/errors'
import { isDatabaseSlowDownError } from '@internal/errors/database-error'
import { FastifyReply } from 'fastify/types/reply'
import { FastifyRequest } from 'fastify/types/request'
import { DatabaseError } from 'pg'

type ValidationIssue = {
instancePath?: string
Expand Down Expand Up @@ -46,16 +46,7 @@ export const s3ErrorHandler = (
}

// database error
if (
error instanceof DatabaseError &&
[
'Max client connections reached',
'remaining connection slots are reserved for non-replication superuser connections',
'no more connections allowed',
'sorry, too many clients already',
'server login has been failing, try again later',
].some((msg) => (error as DatabaseError).message.includes(msg))
) {
if (isDatabaseSlowDownError(error)) {
return reply.status(429).send({
Error: {
Resource: resource,
Expand Down
9 changes: 0 additions & 9 deletions src/internal/database/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,6 @@ export class TenantConnection {
throw ERRORS.DatabaseTimeout(e)
}

// Handle database connection limit errors
if (
e instanceof DatabaseError &&
((e.code === '08P01' && e.message.includes('no more connections allowed')) ||
e.message.includes('Max client connections reached'))
) {
throw ERRORS.DatabaseConnectionLimit(e)
}

throw e
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/internal/errors/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export enum ErrorCode {
KeyAlreadyExists = 'KeyAlreadyExists',
BucketAlreadyExists = 'BucketAlreadyExists',
DatabaseTimeout = 'DatabaseTimeout',
DatabaseConnectionLimit = 'DatabaseConnectionLimit',
DatabaseReadOnly = 'DatabaseReadOnly',
DatabaseInvalidObjectDefinition = 'DatabaseInvalidObjectDefinition',
DatabaseSchemaMismatch = 'DatabaseSchemaMismatch',
Expand Down Expand Up @@ -386,15 +385,6 @@ export const ERRORS = {
originalError: e,
}),

DatabaseConnectionLimit: (e?: Error) =>
new StorageBackendError({
code: ErrorCode.DatabaseConnectionLimit,
httpStatusCode: 503,
message:
'The database has reached its maximum number of connections. Please try again later.',
originalError: e,
}),

DatabaseReadOnly: (e?: Error) =>
new StorageBackendError({
code: ErrorCode.DatabaseReadOnly,
Expand Down
17 changes: 17 additions & 0 deletions src/internal/errors/database-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DatabaseError } from 'pg'

export function isDatabaseSlowDownError(error: Error): boolean {
return (
error instanceof DatabaseError &&
[
'Authentication error', // supavisor specific
'Max client connections reached',
'remaining connection slots are reserved for non-replication superuser connections',
'no more connections allowed',
'sorry, too many clients already',
'server login has been failing, try again later',
'server login has been failing, cached error: connect timeout (server_login_retry)',
'server login has been failing, cached error: the database system is not accepting connections (server_login_retry)',
].some((msg) => error.message.includes(msg))
)
}
Loading