Coder Social home page Coder Social logo

Comments (1)

dosubot avatar dosubot commented on June 21, 2024

πŸ€–

Hello @cayller! I'm Dosu, a friendly bot here to assist you with your Kratos-related queries and concerns while we wait for a human maintainer. I'm here to help you solve bugs, answer questions, and guide you on becoming a contributor. Feel free to share more about your issue and I'll do my best to assist you!

The issue you're experiencing seems to be related to the default error decoder in Kratos. The DefaultErrorDecoder function is responsible for decoding HTTP errors. If the HTTP status code is between 200 and 299, it returns nil, indicating no error. However, for other status codes, it attempts to read the response body and unmarshal it into an errors.Error type. If this process fails, it returns a new error with the status code and an unknown reason.

The problem arises when the context is canceled. In such cases, the HTTP client's Do method returns an error, which is then passed to the DefaultErrorDecoder. However, since the context is canceled, the response is nil, and the decoder cannot read the body or unmarshal it into an errors.Error type. As a result, it returns a new error with the status code and an unknown reason, which is why you're seeing "ctx canceled" instead of the actual error description.

To resolve this issue, you can create a custom error decoder that handles context cancellation errors separately. Here's an example:

func CustomErrorDecoder(_ context.Context, res *http.Response) error {
    if res.StatusCode >= 200 && res.StatusCode <= 299 {
        return nil
    }
    if res == nil {
        return errors.New("Context was canceled")
    }
    defer res.Body.Close()
    data, err := io.ReadAll(res.Body)
    if err == nil {
        e := new(errors.Error)
        if err = CodecForResponse(res).Unmarshal(data, e); err == nil {
            e.Code = int32(res.StatusCode)
            return e
        }
    }
    return errors.Newf(res.StatusCode, errors.UnknownReason, "").WithCause(err)
}

Then, when creating your client, use the WithErrorDecoder option to use your custom error decoder:

client, err := NewClient(ctx, WithErrorDecoder(CustomErrorDecoder))

This way, when the context is canceled, your custom error decoder will return a more descriptive error message. You can find more information about this in the transport/http/client.go file in the Kratos repository.

Sources

from kratos.

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.