Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Overriding app.ErrorMiddleware has no effect #1466

Closed
devboy opened this issue Nov 23, 2018 · 8 comments
Closed

Overriding app.ErrorMiddleware has no effect #1466

devboy opened this issue Nov 23, 2018 · 8 comments
Assignees
Labels
breaking change This feature / fix introduces breaking changes bug Something isn't working help wanted Feel free to contribute!
Milestone

Comments

@devboy
Copy link

devboy commented Nov 23, 2018

I have a buffalo.App for a JSON REST api.
Switching the GO_ENV to production causes all errors to be sent as HTML.
I changed the app.ErrorMiddleware to my custom one yet the defaultErrorMiddleware is still being called while the one I assigned is ignored.

@markbates markbates added the help wanted Feel free to contribute! label Dec 10, 2018
@paganotoni paganotoni self-assigned this Jan 4, 2019
@paganotoni
Copy link
Member

@devboy i tried to reproduce this with https://gist.github.com/paganotoni/a6d165e2a12447759cc9a68518b720a3 and it seems that i'm able to override the default error handlers even with GO_ENV=production.

Instead of changing the ErrorMiddleware i'm updating the error handlers, would this strategy work for you?.

@paganotoni
Copy link
Member

@devboy can i know the reasons to override the ErrorMiddleware and why using custom error handlers wont work for you ?

@sebd71
Copy link

sebd71 commented Jan 29, 2019

I don't understand the goal of if statement of app.go line 55

buffalo/app.go

Line 55 in 78d649a

if a.ErrorMiddleware != nil {

We just create "a" object (above lines 42-52) without setting ErrorMiddleware.
So if statement is useless to my POV (always false).

As said in blog.gobuffalo.io (here section New Error Handling Middleware), it would be useful to take complete control over all of the error handling. But in fact, it's not that is currently implemented.
Method based on ErrorHandlers may be a good solution to override only 1 or 2 specific error but it would be good to also allow to override error middleware.

If you're agree with that, let me know if you want that I submit a pull request.

@markbates markbates added bug Something isn't working breaking change This feature / fix introduces breaking changes labels Feb 3, 2019
@markbates markbates self-assigned this Feb 3, 2019
@markbates markbates added this to the v0.14.0 milestone Feb 3, 2019
@markbates
Copy link
Member

@sebd71 @paganotoni can you both review #1563 to fix this problem we have to introduce a breaking change in v0.14.0 to make it work.

@markbates
Copy link
Member

Fixed with #1564 (v0.14.0-rc.1 - coming this week)

sebd71 added a commit to sebd71/buffalo that referenced this issue Feb 7, 2019
Don't expose internal ErrorHandlers map and declare Get/Set methods to
manage registered ErrorHandlers.

- make ErrorHandlers Get function more robust (handle nil)
- introduce Clear, Clone, Set and SetMulti functions
- export DefaultErrorHandler function so that user can call it from its
  handler if needed be
- add a new test to check Clear and SetMulti functions

Change-Id: Ida701f6c6dea202db788f1dd805e2b7ccdff14cb
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
@sebd71
Copy link

sebd71 commented Feb 7, 2019

Your changes introduce following problems / dangerous behaviors :

1/ you can create deadlock (forever loop)

app.ErrorHandlers.Default(nil)  // Get will return nil producing forever loop

2/ you can reset default handler unintentionally depending on the code order :

app.ErrorHandlers.Default(myDefaultErrorHandler)  // no effect, reset by next lines
app.ErrorHandlers = buffalo.ErrorHandlers{
    404: myErrorHandler,
    500: myErrorHandler,
}
app.ErrorHandlers.Default(myDefaultErrorHandler)  // works fine

So make ErrorHandler private seems to me safest and expose (like you did in middleware) Get / Set functions to manage ErrorHandlers.
Please review the pull request I just push #1578

@markbates
Copy link
Member

Thanks for the feedback. I’m not convinced this needs the full rewrite you propose. The first issue can be fixed with a simple nil check. The second one is expected behavior. If I replace the whole ErrorHandlers with a new one, I would expect it to replace any previous changes I made to it.

I need more convincing that the changes you propose are the right ones.

@markbates
Copy link
Member

I should also point out that you’re requesting breaking changes. I would recommend you open a proposal ticket and lay out your ideas there so the community can discuss the pros/cons of these changes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking change This feature / fix introduces breaking changes bug Something isn't working help wanted Feel free to contribute!
Projects
None yet
Development

No branches or pull requests

4 participants