Skip to content

Commit

Permalink
qe: Identify which request in a batch caused error
Browse files Browse the repository at this point in the history
Adds `batch_request_idx` property to user facing errors. On the client,
that would allow us to build correct error message for `$transaction`
errors.

Ref: prisma/prisma#15433, prisma/prisma#14373
  • Loading branch information
SevInf committed Nov 11, 2022
1 parent a93b663 commit 26e00ab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions libs/user-facing-errors/src/lib.rs
Expand Up @@ -68,6 +68,9 @@ pub struct Error {
is_panic: bool,
#[serde(flatten)]
inner: ErrorType,

#[serde(skip_serializing_if = "Option::is_none")]
batch_request_idx: Option<usize>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Error {
backtrace: Some(format!("{:?}", backtrace::Backtrace::new())),
}),
is_panic: false,
batch_request_idx: None,
}
}

Expand All @@ -110,6 +114,7 @@ impl Error {
backtrace: None,
}),
is_panic: false,
batch_request_idx: None,
}
}

Expand All @@ -135,6 +140,7 @@ impl Error {
backtrace,
}),
is_panic: true,
batch_request_idx: None,
}
}

Expand All @@ -143,6 +149,7 @@ impl Error {
Error {
inner: ErrorType::Known(err),
is_panic: false,
batch_request_idx: None,
}
}

Expand All @@ -155,6 +162,7 @@ impl Error {
backtrace: None,
}),
is_panic: true,
batch_request_idx: None,
}
}

Expand All @@ -172,6 +180,10 @@ impl Error {
err @ ErrorType::Unknown(_) => panic!("Expected known error, got {:?}", err),
}
}

pub fn set_batch_request_idx(&mut self, batch_request_idx: usize) {
self.batch_request_idx = Some(batch_request_idx)
}
}

pub fn new_backtrace() -> backtrace::Backtrace {
Expand All @@ -183,6 +195,7 @@ impl From<UnknownError> for Error {
Error {
inner: ErrorType::Unknown(unknown_error),
is_panic: false,
batch_request_idx: None,
}
}
}
Expand All @@ -192,6 +205,7 @@ impl From<KnownError> for Error {
Error {
is_panic: false,
inner: ErrorType::Known(known_error),
batch_request_idx: None,
}
}
}
9 changes: 9 additions & 0 deletions query-engine/core/src/error.rs
Expand Up @@ -70,6 +70,9 @@ pub enum CoreError {

#[error("{}", _0)]
FieldConversionError(#[from] FieldConversionError),

#[error("Error in batch request {request_idx}: {error}")]
BatchError { request_idx: usize, error: Box<CoreError> },
}

impl CoreError {
Expand Down Expand Up @@ -269,6 +272,12 @@ impl From<CoreError> for user_facing_errors::Error {
.into()
}

CoreError::BatchError { request_idx, error } => {
let mut inner_error = user_facing_errors::Error::from(*error);
inner_error.set_batch_request_idx(request_idx);
inner_error
}

_ => user_facing_errors::Error::from_dyn_error(&err),
}
}
Expand Down
15 changes: 12 additions & 3 deletions query-engine/core/src/executor/execute_operation.rs
Expand Up @@ -44,16 +44,25 @@ pub async fn execute_many_operations(

let mut results = Vec::with_capacity(queries.len());

for (query_graph, serializer) in queries {
for (i, (query_graph, serializer)) in queries.into_iter().enumerate() {
increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL);
let operation_timer = Instant::now();
let interpreter = QueryInterpreter::new(conn);
let result = QueryPipeline::new(query_graph, interpreter, serializer)
.execute(trace_id.clone())
.await?;
.await;

histogram!(PRISMA_CLIENT_QUERIES_HISTOGRAM_MS, operation_timer.elapsed());
results.push(Ok(result));

match result {
Ok(result) => results.push(Ok(result)),
Err(error) => {
return Err(crate::CoreError::BatchError {
request_idx: i,
error: Box::new(error),
});
}
}
}

Ok(results)
Expand Down

0 comments on commit 26e00ab

Please sign in to comment.