11use async_trait:: async_trait;
2- use serde:: Serialize ;
2+ use reqwest:: { Response , StatusCode } ;
3+ use serde:: { Deserialize , Serialize } ;
34use thiserror:: Error ;
45
56use crate :: user_management:: { PasswordReset , UserManagement } ;
@@ -13,15 +14,55 @@ pub struct CreatePasswordResetParams<'a> {
1314}
1415
1516/// An error returned from [`CreatePasswordReset`].
16- #[ derive( Debug , Error ) ]
17- pub enum CreatePasswordResetError { }
17+ #[ derive( Debug , Error , Deserialize ) ]
18+ #[ serde( tag = "code" , rename_all = "snake_case" ) ]
19+ pub enum CreatePasswordResetError {
20+ /// Entity not found error.
21+ #[ error( "entity_not_found: {message}" ) ]
22+ EntityNotFound {
23+ /// A human-readable message describing the error.
24+ message : String ,
25+
26+ /// The entity ID.
27+ entity_id : String ,
28+ } ,
29+ }
1830
1931impl From < CreatePasswordResetError > for WorkOsError < CreatePasswordResetError > {
2032 fn from ( err : CreatePasswordResetError ) -> Self {
2133 Self :: Operation ( err)
2234 }
2335}
2436
37+ #[ async_trait]
38+ pub ( crate ) trait HandleCreatePasswordResetError
39+ where
40+ Self : Sized ,
41+ {
42+ async fn handle_create_password_reset_error (
43+ self ,
44+ ) -> WorkOsResult < Self , CreatePasswordResetError > ;
45+ }
46+
47+ #[ async_trait]
48+ impl HandleCreatePasswordResetError for Response {
49+ async fn handle_create_password_reset_error (
50+ self ,
51+ ) -> WorkOsResult < Self , CreatePasswordResetError > {
52+ match self . error_for_status_ref ( ) {
53+ Ok ( _) => Ok ( self ) ,
54+ Err ( err) => match err. status ( ) {
55+ Some ( StatusCode :: NOT_FOUND ) => {
56+ let error = self . json :: < CreatePasswordResetError > ( ) . await ?;
57+
58+ Err ( WorkOsError :: Operation ( error) )
59+ }
60+ _ => Err ( WorkOsError :: RequestError ( err) ) ,
61+ } ,
62+ }
63+ }
64+ }
65+
2566/// [WorkOS Docs: Create a password reset token](https://workos.com/docs/reference/user-management/password-reset/create)
2667#[ async_trait]
2768pub trait CreatePasswordReset {
@@ -74,7 +115,9 @@ impl CreatePasswordReset for UserManagement<'_> {
74115 . json ( & params)
75116 . send ( )
76117 . await ?
77- . handle_unauthorized_or_generic_error ( ) ?
118+ . handle_unauthorized_error ( ) ?
119+ . handle_create_password_reset_error ( )
120+ . await ?
78121 . json :: < PasswordReset > ( )
79122 . await ?;
80123
0 commit comments