@@ -4,7 +4,6 @@ use crate::{
44 db:: PoolError ,
55 web:: { page:: WebPage , releases:: Search , AxumErrorPage , ErrorPage } ,
66} ;
7- use anyhow:: Context as _;
87use axum:: {
98 http:: StatusCode ,
109 response:: { IntoResponse , Response as AxumResponse } ,
@@ -157,7 +156,7 @@ pub enum AxumNope {
157156 #[ error( "Internal server error" ) ]
158157 InternalServerError ,
159158 #[ error( "internal error" ) ]
160- InternalError ( # [ from ] anyhow:: Error ) ,
159+ InternalError ( anyhow:: Error ) ,
161160}
162161
163162impl IntoResponse for AxumNope {
@@ -235,6 +234,18 @@ impl IntoResponse for AxumNope {
235234 }
236235}
237236
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+
238249impl From < Nope > for AxumNope {
239250 fn from ( err : Nope ) -> Self {
240251 match err {
@@ -251,44 +262,6 @@ impl From<Nope> for AxumNope {
251262
252263pub ( crate ) type AxumResult < T > = Result < T , AxumNope > ;
253264
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-
292265#[ cfg( test) ]
293266mod tests {
294267 use crate :: test:: wrapper;
0 commit comments