Skip to content

Commit

Permalink
qe: Map "Too many connections" errors to code P2037 (#4724)
Browse files Browse the repository at this point in the history
* qe: Map "Too many connections" errors to code P2037

Contributes to prisma/team-orm#945

* Update libs/user-facing-errors/src/query_engine/mod.rs

Co-authored-by: Flavian Desverne <[email protected]>

* Preserve original error

---------

Co-authored-by: Flavian Desverne <[email protected]>
  • Loading branch information
SevInf and Weakky authored Feb 13, 2024
1 parent 7efa9cb commit 57cc2f6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libs/user-facing-errors/src/query_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,9 @@ pub struct ExternalError {
/// id of the error in external system, which would allow to retrieve it later
pub id: i32,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P2037", message = "Too many database connections opened: {message}")]
pub struct TooManyConnections {
pub message: String,
}
7 changes: 7 additions & 0 deletions quaint/src/connector/mssql/native/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ impl From<tiberius::error::Error> for Error {

builder.build()
}
tiberius::error::Error::Server(e) if e.code() == 5828 => {
let mut builder = Error::builder(ErrorKind::TooManyConnections(e.clone().into()));
builder.set_original_code(format!("{}", e.code()));
builder.set_original_message(e.message().to_string());

builder.build()
}
tiberius::error::Error::Server(e) => {
let kind = ErrorKind::QueryError(e.clone().into());

Expand Down
6 changes: 6 additions & 0 deletions quaint/src/connector/mysql/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ impl From<MysqlError> for Error {
builder.set_original_message(error.message);
builder.build()
}
1040 | 1203 => {
let mut builder = Error::builder(ErrorKind::TooManyConnections(error.clone().into()));
builder.set_original_code(format!("{code}"));
builder.set_original_message(error.message);
builder.build()
}
_ => {
let kind = ErrorKind::QueryError(
MysqlAsyncError::Server(MysqlError {
Expand Down
10 changes: 10 additions & 0 deletions quaint/src/connector/postgres/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ impl From<PostgresError> for Error {
builder.build()
}

"53300" => {
let code = value.code.to_owned();
let message = value.to_string();
let kind = ErrorKind::TooManyConnections(value.into());
let mut builder = Error::builder(kind);
builder.set_original_code(code);
builder.set_original_message(message);
builder.build()
}

_ => {
let code = value.code.to_owned();
let message = value.to_string();
Expand Down
3 changes: 3 additions & 0 deletions quaint/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub enum ErrorKind {
#[error("Error querying the database: {}", _0)]
QueryError(Box<dyn std::error::Error + Send + Sync + 'static>),

#[error("Too many DB connections opened")]
TooManyConnections(Box<dyn std::error::Error + Send + Sync + 'static>),

#[error("Invalid input provided to query: {}", _0)]
QueryInvalidInput(String),

Expand Down
9 changes: 9 additions & 0 deletions query-engine/connectors/query-connector/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ impl ConnectorError {
ErrorKind::RecordDoesNotExist { cause } => Some(KnownError::new(
user_facing_errors::query_engine::RecordRequiredButNotFound { cause: cause.clone() },
)),

ErrorKind::TooManyConnections(e) => Some(user_facing_errors::KnownError::new(
user_facing_errors::query_engine::TooManyConnections {
message: format!("{}", e),
},
)),
_ => None,
};

Expand Down Expand Up @@ -278,6 +284,9 @@ pub enum ErrorKind {

#[error("Invalid driver adapter: {0}")]
InvalidDriverAdapter(String),

#[error("Too many DB connections opened: {}", _0)]
TooManyConnections(Box<dyn std::error::Error + Send + Sync>),
}

impl From<DomainError> for ConnectorError {
Expand Down
5 changes: 5 additions & 0 deletions query-engine/connectors/sql-query-connector/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ pub enum SqlError {

#[error("External connector error")]
ExternalError(i32),

#[error("Too many DB connections opened")]
TooManyConnections(Box<dyn std::error::Error + Send + Sync>),
}

impl SqlError {
Expand Down Expand Up @@ -282,6 +285,7 @@ impl SqlError {
SqlError::MissingFullTextSearchIndex => ConnectorError::from_kind(ErrorKind::MissingFullTextSearchIndex),
SqlError::InvalidIsolationLevel(msg) => ConnectorError::from_kind(ErrorKind::InternalConversionError(msg)),
SqlError::ExternalError(error_id) => ConnectorError::from_kind(ErrorKind::ExternalError(error_id)),
SqlError::TooManyConnections(e) => ConnectorError::from_kind(ErrorKind::TooManyConnections(e)),
}
}
}
Expand Down Expand Up @@ -336,6 +340,7 @@ impl From<quaint::error::Error> for SqlError {
QuaintKind::TransactionWriteConflict => Self::TransactionWriteConflict,
QuaintKind::RollbackWithoutBegin => Self::RollbackWithoutBegin,
QuaintKind::ExternalError(error_id) => Self::ExternalError(error_id),
QuaintKind::TooManyConnections(e) => Self::TooManyConnections(e),
e @ QuaintKind::UnsupportedColumnType { .. } => SqlError::ConversionError(e.into()),
e @ QuaintKind::TransactionAlreadyClosed(_) => SqlError::TransactionAlreadyClosed(format!("{e}")),
e @ QuaintKind::IncorrectNumberOfParameters { .. } => SqlError::QueryError(e.into()),
Expand Down

0 comments on commit 57cc2f6

Please sign in to comment.