Skip to content

Commit ad91542

Browse files
committed
fix(api): handle wrapped operation errors
1 parent 7e81f73 commit ad91542

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

packages/common/api-helper/build/src/error.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chirp_workflow::prelude::WorkflowError;
12
use chrono::TimeZone;
23
use global_error::prelude::*;
34
use headers::HeaderValue;
@@ -26,6 +27,14 @@ pub fn handle_rejection(
2627
mut response: http::response::Builder,
2728
ray_id: Uuid,
2829
) -> Result<Response<Body>, http::Error> {
30+
// TODO: Remove panic
31+
let verbose_errors = config
32+
.server()
33+
.expect("missing server")
34+
.rivet
35+
.api_public
36+
.verbose_errors();
37+
2938
// Log error
3039
let err = match err {
3140
GlobalError::BadRequest { .. } => {
@@ -37,36 +46,43 @@ pub fn handle_rejection(
3746

3847
// Replace internal errors with global errors
3948
// TODO: Remove panic
40-
if config
41-
.server()
42-
.expect("missing server")
43-
.rivet
44-
.api_public
45-
.verbose_errors()
46-
{
49+
if verbose_errors {
4750
err_code!(ERROR, error = err.to_string())
4851
} else {
4952
err_code!(ERROR, error = "An internal error has occurred.",)
5053
}
5154
}
5255
GlobalError::Raw(err) => {
53-
tracing::error!(?err, "internal error response");
56+
// Check if this is a workflow error that wraps a global error
57+
match err.downcast::<WorkflowError>().map(|x| *x) {
58+
Ok(WorkflowError::OperationFailure(global_err)) => {
59+
// Handle unwrapped error
60+
return handle_rejection(config, global_err, response, ray_id);
61+
}
62+
Ok(err) => {
63+
tracing::error!(?err, "internal error response");
5464

55-
// Replace internal errors with global errors
56-
// TODO: Remove panic
57-
if config
58-
.server()
59-
.expect("missing server")
60-
.rivet
61-
.api_public
62-
.verbose_errors()
63-
{
64-
err_code!(ERROR, error = err.to_string())
65-
} else {
66-
err_code!(
67-
ERROR,
68-
error = format!("An internal error has occurred (ray_id {}).", ray_id)
69-
)
65+
if verbose_errors {
66+
err_code!(ERROR, error = err.to_string())
67+
} else {
68+
err_code!(
69+
ERROR,
70+
error = format!("An internal error has occurred (ray_id {}).", ray_id)
71+
)
72+
}
73+
}
74+
Err(err) => {
75+
tracing::error!(?err, "internal error response");
76+
77+
if verbose_errors {
78+
err_code!(ERROR, error = err.to_string())
79+
} else {
80+
err_code!(
81+
ERROR,
82+
error = format!("An internal error has occurred (ray_id {}).", ray_id)
83+
)
84+
}
85+
}
7086
}
7187
}
7288
};

packages/common/formatted-error/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn visit_dir(ctx: &mut Ctx, path: PathBuf) -> std::io::Result<()> {
178178
}
179179
})
180180
.collect::<String>();
181-
let documentation = format!("https://rivet.gg/docs/general/errors#{clean_title}");
181+
let documentation = format!("https://rivet.gg/docs/api/errors#{clean_title}");
182182

183183
ctx.hash_items.push(formatdoc!(
184184
"

0 commit comments

Comments
 (0)