if !strings.HasPrefix(mediaType, "application/json") {
// bogus media type, deal
return HttpErr{Err: errors.New(""), Status: http.StatusUnsupportedMediaType}
}
Sorry, but having dealt with a HTTP library in PHP that did something similar, I would have to hunt you down and kill you. The HTTP response status code from the original request was NOT 415 Unsupported Media Type. Do you have any idea how misleading this error is to the developer? The error you SHOULD be returning is an Application error code "Unexpected Media Type", NOT a HTTP error code. It's the application that has a problem with the returned media type.Attempts at off-the-cuff witty death threats don't tend to work too well online. I truly hope you were jesting.
Always code as if the guy who ends up maintaining
your code will be a violent psychopath who knows
where you live.My scenario is a web service, that calls other web services.
It throws an error and then has to return something meaningful to the callee, which is usually both a message that tells the developer calling the web service how to fix it (if it's something that they can fix) as well as the relevant response code.
The advantage from assigning the code at the earliest point the error is detected is that one avoids any later text parsing of the error message to figure out the right code to return. It also allows general switching on the error code range to see whether you really need to throw the error or whether there is some other branch of logic that could be done instead.
But at every point where Tim then returns a failure we lose insight as to why something failed.
My preferred idiom for handling all errors when it comes to web requests or responses is to return both the error (as it has the detailed message of what happened) and the http status code that matches the error (as it has translated the message into a code that describes the type of error).
This bit would change:
to: Whatever receives that knows there has been an error, and has the code available to do tell the developer or end user something meaningful without relying on the error message.I'm sure others are also creating their own http error as well, so whilst Go does http really well it seems in practise that there are still a few places where it could benefit from a little extra to help standardise the code we write.
All of those Http codes are here: http://golang.org/pkg/net/http/#pkg-constants
Regarding the map stuff, the only other place the lack of generics has stood out has been within slices of a type of thing.
The scenario is: I have a slice of identifiers in an order important to me, for each of these concurrently fetch the record being identified and create a slice of those records ordered as per the initial slice.
We've ended up using this type of thing:
In that example a Comment is a comment made on a forum web page and we're fetching lots concurrently, but needing to return them in the order that they will be displayed.The problem stems from that envelope, and the need to implement that sort function for every type that we ever want to concurrently fetch and sort.
It's the only place where I think, if we just had generics this could be a lot better.