Skip to content

Add support for customisation of error response payload #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add support for customisation of error response payload #182

wants to merge 1 commit into from

Conversation

tobiemh
Copy link

@tobiemh tobiemh commented Feb 14, 2016

Added support for customisation of error responses using a custom error handler function. This allows to customise the format and structure of all error messages generated by go-json-rest, if needed.

This should be more versatile than the pull request suggested in #174, and does not add another method which would probably go unused in most implementations.

Example 1 (inline method)

package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "log"
    "net/http"
)

func main() {
    api := rest.NewApi()
    rest.ErrorHandler = func(w rest.ResponseWriter, code int) {
        w.WriteHeader(code)
        w.WriteJson(code)
    }
    router, err := rest.MakeRouter(
        rest.Trace("/", func(w rest.ResponseWriter, req *rest.Request) {
            w.WriteJson("Trace method called")
        }),
    )
    if err != nil {
        log.Fatal(err)
    }
    api.SetApp(router)
    log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}

func errors(w api.ResponseWriter, code int) {
    w.WriteHeader(code)
    w.WriteJson(code)
}

Example 2 (extensive configuration)

package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "log"
    "net/http"
)

func main() {
    api := rest.NewApi()
    rest.ErrorHandler = errors
    router, err := rest.MakeRouter(
        rest.Trace("/", func(w rest.ResponseWriter, req *rest.Request) {
            w.WriteJson("Trace method called")
        }),
    )
    if err != nil {
        log.Fatal(err)
    }
    api.SetApp(router)
    log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
package main

import (
    "github.com/ant0ine/go-json-rest/rest"
)

func errors(w rest.ResponseWriter, code int) {
    w.WriteHeader(code)
    w.WriteJson(errs[code])
}

var errs = map[int]interface{}{

    404: map[string]interface{}{
        "code":          404,
        "details":       "Request resource not found",
        "information":   "The requested resource does not exist. Check that you have entered the url correctly.",
    },

    405: map[string]interface{}{
        "code":          405,
        "details":       "This method is not allowed",
        "information":   "The requested http method is not allowed for this resource. Refer to the documentation for allowed methods.",
    },

    415: map[string]interface{}{
        "code":          415,
        "details":       "Unsupported content type requested",
        "information":   "Requests to the api must use the 'Content-Type: application/json' header. Check your request settings and try again.",
    },

    500: map[string]interface{}{
        "code":          500,
        "details":       "There was a problem with our servers, and we have been notified",
    },

}

@ant0ine
Copy link
Owner

ant0ine commented Feb 14, 2016

Hi, thanks for the pull request.

If ErrorHandler is defined and the user calls rest.Error(...) withe an error message, then the error message is ignored in favor of the custom handler. This is a confusing API.

I'm not convinced that this is worth the added complexity. If the default Error handler is not good enough for a particular use case, then just write another one.

@tobiemh tobiemh closed this Mar 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants