Comments (12)
You can read more here http://blog.golang.org/error-handling-and-go. In short, I wanted to have a proper error type as convention instead of a generic error. The error object can still be set inside HTTPError
struct and checked against in HTTPErrorHandler
.
from echo.
Isn't the blog saying exactly what I'm saying, which is to leave error
in the signature and pass a more specific type when appropriate? Being able to return err in handlers was one of the niceties of echo IMHO and now it becomes a cumbersome &echo.HttpError{Code:500, Message: ..., C: c}
thing. If HTTPError
implemented Error() then it could be passed as error
but it wouldn't be required when a 500-error is all the user wants. Isn't this exactly what the blog post also says?
from echo.
but it's the right thing to do here because ServeHTTP is the only place that sees the value and uses its contents.
The above statement holds true in this case as well. I am not against anything it's just that I wanted returning HTTPError
to be the convention. You can even just return &HTTPError{}
.
from echo.
Not sure what you're quoting. I guess I get to fork your repo then. What I proposed still would allow you to use HTTPError everywhere you want but doesn't force it. Too bad.
from echo.
@tve It's from the same blog post. As caller of the handlers is ServeHTTP which should only be understanding HTTPError
gives it more meaning. Also, project is already using *HTTPError
from past couple of weeks and changing now will definitely break many people's code.
from echo.
@vishr I think you're missing the point. From the article you are referring to:
A sophisticated caller can then use a type assertion to check for a NegativeSqrtError and handle it specially, while callers that just pass the error to fmt.Println or log.Fatal will see no change in behavior.
The handlers should return error
, then user code can check for *HTTPError
with an assertion and use that if needed. There's a lot of benefit deriving from using built-in types especially for error handling. For example every single implementation of an echo app is going to have to implement some Error()
functiont that creates a *HTTPError
from an error
because that's what every other package use to return errors. I guess you could provide such a function but again that's not idiomatic.
from echo.
@raphael If you look at the end of the post, the author mentions about returning appError in cases like this:
type appError struct {
Error error
Message string
Code int
}
(It's usually a mistake to pass back the concrete type of an error rather than error, for reasons discussed in the Go FAQ, but it's the right thing to do here because ServeHTTP is the only place that sees the value and uses its contents.)
And make
appHandler
's ServeHTTP method display theappError
's Message to the user with the correct HTTP status Code and log the full Error to the developer console:
With HTTPError
in the signature handler signature it brings a good convention. For instance, if all handlers start sending any error (As they don't know about HTTPError
) then the default centralized HTTP error handler won't be of much use, turning everything into 500
.
Returning HTTPError
is very easy with default error, just wrap it like this &HTTPError{Error: err}
Nothing is set in stone, I am open for discussion.
from echo.
Thank you for the pointer, I understand the reference now. I guess I still don't see the benefit, it would be easy for echo to check whether the error is an HTTPError
and if so use that to build the response otherwise call a generic error handler. The way it is now I have to wrap all errors in *HTTPError
(like this), it would seem better if that was done in once place (the generic error handler). In general the code for the app before that change was cleaner.
from echo.
How about if I add support for func (*echo.Context) error
?
from echo.
That would certainly help - I do wonder about how having two ways of defining handlers affects the usability of the framework though (you'd have to explain why there are two ways - when to use one vs. the other etc.). Probably something to ponder before making any change.
from echo.
Fixed in #76.
from echo.
Nice! thank you.
from echo.
Related Issues (20)
- Add an AllowContentType middleware (based on chi)
- Bind() panics when the destination is a map with non-interface types HOT 1
- Add SSE function in Context.Response HOT 7
- Any software design issues HOT 1
- Request support for more routing path matching HOT 1
- Unable to use proxy middleware along with bodyDump middleware HOT 1
- `Host` header always blank HOT 6
- Does echo support i18n? [feature request] HOT 2
- Weirdness with wrapped handler and embed FS (static files) HOT 1
- Remove default charset from 'application/json' Content-Type header
- echo v4 websocket client closed, program exited HOT 3
- router not setting ContextKeyHeaderAllow for group, causing cors preflight to not work HOT 6
- Static file has wrong body returned when using a custom HTTP Error Handler and complex paths HOT 2
- QueryParams seem to override formValues HOT 3
- DefaultHTTPErrorHandler does not log HOT 3
- Support Casbin Authorization Middleware HOT 1
- Echo response object calls flush on unflushable objects HOT 7
- Feature request: support Yaml responses transparently. HOT 1
- static middleware: path ist unescaped twice for file names, leading to not downloadable content
- CSRF middleware form lookup consumes all the request body HOT 1
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 echo.