Comments (6)
I can relate. Now we have to completely manually do Response
parsing if we want to get the body of error response. I suggest passing ErrorResponse
with PapyrusError
as it is done in Retrofit
.
In my perfect world I see it this way:
try {
return try await someAPI.generate()
} catch error as PapyrusError {
var message = error.message
if let someError = try? error.response.body?.decode(SomeError.self, using: jsonDecoder) {
message = someError.messageFromServerDescribingError
}
// show message to user
}
from papyrus.
For the cases I met, used an additional interceptor which decodes specific API error responses. If it fails - default error processor will be used.
let authIterceptor = AuthRequestInterceptor(verifier: authVerifier, updater: authUpdater)
let durationInterceptor = RequestDurationInterceptor()
let authModifier = AuthRequestModifier(authStore: store)
let defaultProvider = Provider(baseURL: API_BASE_URI,
modifiers: [authModifier],
interceptors: [APIErrorInterceptor(), authIterceptor, durationInterceptor])
from papyrus.
Thanks for the feedback @malonehedges!
What's the use case you're thinking of?
Using the body from the raw Response
is currently the only way. The error might not have a body (i.e. if there were a transport error) and having a custom error type on an unsuccessful status code would be tricky to have it built in. You'd need to have two types in the return; an expected Res
type plus the possible Err
type to decode.
You are able to use the build in decode(...)
function on Response
however:
@GET("/error")
func getError() async throws -> Response
let res = try await api.getError()
if let statusCode = res.statusCode, !(200...399).contains(statusCode) {
let errorBody = try res.decode(MyCustomErrorResponse)
// handle custom error response
} else {
// handle success
}
from papyrus.
In my APIs I typically return either 200 with the expected response type, or some error code and { "error": true, "message": "Handle must be between 4 and 16 characters" }
for example, so that I can show that message in the client
from papyrus.
Ultimate goal is to turn a fetch failure into a message, either defined by the API in the response or falling back to "Something went wrong, please try again"
from papyrus.
This is released as of v0.6.2.
from papyrus.
Related Issues (20)
- Add URLSession based driver HOT 2
- Allow returning `Data` as a response type HOT 2
- @JSON encoder argument HOT 1
- Void return value support HOT 1
- Support for default values in Request HOT 2
- PapyrusAlamofire not working HOT 1
- Request Cancellation
- Request Progress
- Request Retry
- Multipart encoding not working HOT 1
- Unable to build project and apply custom JSON encoder and decoder HOT 1
- @JSON has no effect HOT 4
- Log Failed Request cURL HOT 3
- Request streaming HOT 2
- Automatically add default values for optional types HOT 1
- Error when trying to set path parameter in front of static query HOT 2
- Support for Vapor? HOT 3
- Ambiguous use of 'addParameter(_:value:)'
- Path parameter in filename not working HOT 1
- Xcode Cloud trust issue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from papyrus.