This repository was archived by the owner on May 13, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11use thiserror:: Error ;
22
3+ /// A JSON or text body.
4+ #[ derive( Clone , Debug ) ]
5+ pub enum JsonOrText {
6+ /// JSON body.
7+ Json ( serde_json:: Value ) ,
8+
9+ /// Text body.
10+ Text ( String ) ,
11+ }
12+
313/// A WorkOS SDK error.
414#[ derive( Debug , Error ) ]
515pub enum WorkOsError < E > {
@@ -11,6 +21,16 @@ pub enum WorkOsError<E> {
1121 #[ error( "unauthorized" ) ]
1222 Unauthorized ,
1323
24+ /// An unknown error response was received from the WorkOS API.
25+ #[ error( "unknown error" ) ]
26+ Unknown {
27+ /// The response status code.
28+ status : reqwest:: StatusCode ,
29+
30+ /// The response body.
31+ body : JsonOrText ,
32+ } ,
33+
1434 /// An error occurred while parsing a URL.
1535 #[ error( "URL parse error" ) ]
1636 UrlParseError ( #[ from] url:: ParseError ) ,
@@ -22,10 +42,6 @@ pub enum WorkOsError<E> {
2242 /// An unhandled error occurred with the API request.
2343 #[ error( "request error" ) ]
2444 RequestError ( #[ from] reqwest:: Error ) ,
25-
26- /// The API responded with an error.
27- #[ error( "API error" ) ]
28- ApiError ( serde_json:: Value ) ,
2945}
3046
3147/// A WorkOS SDK result.
Original file line number Diff line number Diff line change 11use reqwest:: { Response , StatusCode } ;
22
3- use crate :: { WorkOsError , WorkOsResult } ;
3+ use crate :: { JsonOrText , WorkOsError , WorkOsResult } ;
44
55pub trait ResponseExt
66where
@@ -37,12 +37,18 @@ impl ResponseExt for Response {
3737 . is_some_and ( |value| value. to_lowercase ( ) . starts_with ( "application/json" ) )
3838 {
3939 match self . json ( ) . await {
40- Ok ( value) => Err ( WorkOsError :: ApiError ( value) ) ,
40+ Ok ( value) => Err ( WorkOsError :: Unknown {
41+ status,
42+ body : JsonOrText :: Json ( value) ,
43+ } ) ,
4144 Err ( err) => Err ( WorkOsError :: RequestError ( err) ) ,
4245 }
4346 } else {
44- match self . error_for_status ( ) {
45- Ok ( response) => Ok ( response) ,
47+ match self . text ( ) . await {
48+ Ok ( text) => Err ( WorkOsError :: Unknown {
49+ status,
50+ body : JsonOrText :: Text ( text) ,
51+ } ) ,
4652 Err ( err) => Err ( WorkOsError :: RequestError ( err) ) ,
4753 }
4854 }
You can’t perform that action at this time.
0 commit comments