Skip to content

Commit aba388d

Browse files
authored
fix: ease the error type that is returned by a worker function (#577)
* fix: ease the error type that is returned * chore: add changelog
1 parent d80d5f9 commit aba388d

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ All notable changes to this project are documented in this file.
1212
- **deps** : update rust crate redis to 0.31 ([#555](https://github.com/geofmureithi/apalis/pull/555))
1313
- **deps** : update rust crate sentry-core to 0.38.0 ([#569](https://github.com/geofmureithi/apalis/pull/569))
1414
- **deps** : update rust crate criterion to 0.6.0 ([#574](https://github.com/geofmureithi/apalis/pull/574))
15-
16-
17-
## Fixed
18-
15+
- **error-handling** : ease the error type that is returned by a worker function ([#577](https://github.com/geofmureithi/apalis/pull/577))
1916
- **PostgresStorage**: fix type error when updating jobs ([#539](https://github.com/geofmureithi/apalis/issues/539))
2017

2118
## [0.7.1](https://github.com/geofmureithi/apalis/releases/tag/v0.7.1)

examples/basics/src/main.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ async fn produce_jobs(storage: &SqliteStorage<Email>) {
2929
}
3030
}
3131

32-
#[derive(thiserror::Error, Debug)]
33-
pub enum ServiceError {
34-
#[error("data store disconnected")]
35-
Disconnect(#[from] std::io::Error),
36-
#[error("the data for key `{0}` is not available")]
37-
Redaction(String),
38-
#[error("invalid header (expected {expected:?}, found {found:?})")]
39-
InvalidHeader { expected: String, found: String },
40-
#[error("unknown data store error")]
41-
Unknown,
42-
}
43-
4432
#[derive(thiserror::Error, Debug)]
4533
pub enum PanicError {
4634
#[error("{0}")]
@@ -54,7 +42,7 @@ async fn send_email(
5442
svc: Data<EmailService>,
5543
worker: Worker<Context>,
5644
cache: Data<ValidEmailCache>,
57-
) -> Result<(), ServiceError> {
45+
) -> Result<(), BoxDynError> {
5846
info!("Job started in worker {:?}", worker.id());
5947
let cache_clone = cache.clone();
6048
let email_to = email.to.clone();

packages/apalis-core/src/response.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{any::Any, fmt::Debug, sync::Arc};
1+
use std::{fmt::Debug, sync::Arc};
22

33
use crate::{
4-
error::Error,
4+
error::{BoxDynError, Error},
55
task::{attempt::Attempt, task_id::TaskId},
66
};
77

@@ -133,21 +133,17 @@ impl IntoResponse for bool {
133133
}
134134
}
135135

136-
impl<T: Any, E: std::error::Error + Sync + Send + 'static + Any> IntoResponse
137-
for std::result::Result<T, E>
138-
{
136+
impl<T, E: Into<BoxDynError>> IntoResponse for std::result::Result<T, E> {
139137
type Result = Result<T, Error>;
140138
fn into_response(self) -> Result<T, Error> {
141139
match self {
142140
Ok(value) => Ok(value),
143141
Err(e) => {
144-
// Try to downcast the error to see if it is already of type `Error`
145-
if let Some(custom_error) =
146-
(&e as &(dyn std::error::Error + 'static)).downcast_ref::<Error>()
147-
{
142+
let e = e.into();
143+
if let Some(custom_error) = e.downcast_ref::<Error>() {
148144
return Err(custom_error.clone());
149145
}
150-
Err(Error::Failed(Arc::new(Box::new(e))))
146+
Err(Error::Failed(Arc::new(e)))
151147
}
152148
}
153149
}

0 commit comments

Comments
 (0)