GithubHelp home page GithubHelp logo

Comments (6)

F0x1d avatar F0x1d commented on August 17, 2024 1

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.

ucomah avatar ucomah commented on August 17, 2024 1

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.

joshuawright11 avatar joshuawright11 commented on August 17, 2024

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.

malonehedges avatar malonehedges commented on August 17, 2024

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.

malonehedges avatar malonehedges commented on August 17, 2024

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.

joshuawright11 avatar joshuawright11 commented on August 17, 2024

Thanks @F0x1d for the PR 💯

This is released as of v0.6.2.

from papyrus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.