@@ -4,7 +4,6 @@ use crate::{
4
4
db:: PoolError ,
5
5
web:: { page:: WebPage , releases:: Search , AxumErrorPage , ErrorPage } ,
6
6
} ;
7
- use anyhow:: Context as _;
8
7
use axum:: {
9
8
http:: StatusCode ,
10
9
response:: { IntoResponse , Response as AxumResponse } ,
@@ -157,7 +156,7 @@ pub enum AxumNope {
157
156
#[ error( "Internal server error" ) ]
158
157
InternalServerError ,
159
158
#[ error( "internal error" ) ]
160
- InternalError ( # [ from ] anyhow:: Error ) ,
159
+ InternalError ( anyhow:: Error ) ,
161
160
}
162
161
163
162
impl IntoResponse for AxumNope {
@@ -235,6 +234,18 @@ impl IntoResponse for AxumNope {
235
234
}
236
235
}
237
236
237
+ impl From < anyhow:: Error > for AxumNope {
238
+ fn from ( err : anyhow:: Error ) -> Self {
239
+ match err. downcast :: < AxumNope > ( ) {
240
+ Ok ( axum_nope) => axum_nope,
241
+ Err ( err) => match err. downcast :: < Nope > ( ) {
242
+ Ok ( iron_nope) => AxumNope :: from ( iron_nope) ,
243
+ Err ( err) => AxumNope :: InternalError ( err) ,
244
+ } ,
245
+ }
246
+ }
247
+ }
248
+
238
249
impl From < Nope > for AxumNope {
239
250
fn from ( err : Nope ) -> Self {
240
251
match err {
@@ -251,44 +262,6 @@ impl From<Nope> for AxumNope {
251
262
252
263
pub ( crate ) type AxumResult < T > = Result < T , AxumNope > ;
253
264
254
- /// spawn-blocking helper for usage in axum requests.
255
- ///
256
- /// Should be used when spawning tasks from inside web-handlers
257
- /// that return AxumNope as error. The join-error will also be
258
- /// converted into an `anyhow::Error`. Any standard AxumNope
259
- /// will be left intact so the error-handling can generate the
260
- /// correct status-page with the correct status code.
261
- ///
262
- /// Examples:
263
- ///
264
- /// with standard `tokio::task::spawn_blocking`:
265
- /// ```ignore
266
- /// let data = spawn_blocking(move || -> Result<_, AxumNope> {
267
- /// let data = get_the_data()?;
268
- /// Ok(data)
269
- /// })
270
- /// .await
271
- /// .context("failed to join thread")??;
272
- /// ```
273
- ///
274
- /// with this helper function:
275
- /// ```ignore
276
- /// let data = spawn_blocking(move || {
277
- /// let data = get_the_data()?;
278
- /// Ok(data)
279
- /// })
280
- /// .await?
281
- /// ```
282
- pub ( crate ) async fn spawn_blocking_web < F , R > ( f : F ) -> AxumResult < R >
283
- where
284
- F : FnOnce ( ) -> AxumResult < R > + Send + ' static ,
285
- R : Send + ' static ,
286
- {
287
- tokio:: task:: spawn_blocking ( f)
288
- . await
289
- . context ( "failed to join thread" ) ?
290
- }
291
-
292
265
#[ cfg( test) ]
293
266
mod tests {
294
267
use crate :: test:: wrapper;
0 commit comments