Skip to content
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

Some improvements to request error handling and modal error formatting #1929

Merged
merged 1 commit into from
May 12, 2020

Conversation

dsevillamartin
Copy link
Member

@dsevillamartin dsevillamartin commented Nov 9, 2019

Changes proposed in this pull request:

  • Only show error(s) detail in debug modal, if found
  • Log the request URL, method, status, and response
    • Only logs error(s) detail if found, else it logs the whole error object
    • Logs with a group so the error data can be hidden

Reviewers should focus on:
The alert appearing was causing the modal to "close" but the background overlay remained... not sure if that's a bug or intended (?) so I closed the currently opened modal - though I don't think want to do that.

Screenshot

POST /api/flags 404

image
image
image

POST /flags 404

image
image

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).

@luceos
Copy link
Member

luceos commented Nov 11, 2019

Won't closing the currently active Modal cause unwanted side effects? I can imagine this would happen on log in, register etc; even so.. can't we make the error be pushed on top of the overlay somehow?

Ps, I also assume that changing these outputs will break all extensions returning custom error output?

@dsevillamartin
Copy link
Member Author

@luceos

Won't closing the currently active Modal cause unwanted side effects?

Perhaps. The problem isn't the error alert being behind the modal, is that showing the alert results in the modal fade out animation to trigger, but the backdrop remains. Might be a separate issue though.

Ps, I also assume that changing these outputs will break all extensions returning custom error output?

I don't understand what you are saying here. The formattedError variable is only used for the error logging and for showing it in the RequestErrorModal - what extensions would be using it?

@luceos
Copy link
Member

luceos commented Nov 15, 2019

Ah your answer clarifies it. Then I have nothing to add ;)

@luceos
Copy link
Member

luceos commented Nov 18, 2019

Asked for second review from @clarkwinkelmann ; if approved feel free to merge.

Copy link
Member

@clarkwinkelmann clarkwinkelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great, but I have some concerns:

Why use unescape() which is deprecated ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape . Why is a decoding necessary at all ?

What is the purpose of the new this.modal.close() ? It sounds like if any error is triggered while a modal is open and the request isn't bound to that modal handler, it would close it, potentially losing data. I think core always uses the error handling inside the modal, but extension might do their own requests from the modal.

I wanted to test this with fof/terms for example. It means an error fetching the list of policies, reordering, or during saving would close the modal. But I couldn't test because of the issue I discussed on Discord where I can't install terms on my master Flarum 🤷‍♂️

@dsevillamartin
Copy link
Member Author

@clarkwinkelmann Escape is required to transform \n into actual line breaks.

I changed the function call to decodeURI and removed the closing of the modal - not sure why, but I can no longer reproduce what I first had happen.

@clarkwinkelmann
Copy link
Member

Those are great cases for inline comments I think 😄

If newlines are the only reason to use it, wouldn't .replace() work just as well ?

If we keep it like it's now I suggest adding

Escape is required to transform \n into actual line breaks

as a comment near decodeURI so we know why it was used in the future.

Otherwise approved 👍


error.alert = new Alert({
type: 'error',
children,
controls: isDebug && [
<Button className="Button Button--link" onclick={this.showDebug.bind(this, error)}>Debug</Button>
<Button className="Button Button--link" onclick={this.showDebug.bind(this, error, formattedError)}>Debug</Button>
]
});

try {
options.errorHandler(error);
} catch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah, can we rename this error variable in the catch block? I find it super confusing to follow along which object we're dealing with here. 😁


🕐 Three minutes later...


Okay, I see now, the default errorHandler implementation just re-throws the same object, which means we have an AJAX error, which makes sense.

Could we change this to check for the existence of options.errorHandler() instead? The try/catch only confuses the reader IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Had to remove the default errorHandler implementationa as it's not needed.

I noticed that when the error handler is set - e.g. modals using this.onerror - nothing logs to the console. I assume we want to log the error to the console regardless if the error handler is set?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, no, I think we should reserve the logging to unexpected errors - exactly the way you did it.

Really, it depends on whether we're dealing with a known or unknown error, but we have no mechanism for that yet (in custom error handlers).

@clarkwinkelmann
Copy link
Member

clarkwinkelmann commented Feb 24, 2020

I have still not had time to test this locally, but from reading the code:

Aren't we losing the feature of the errorHandler function intentionally not handling an error ?

I'm actually using this feature in my Formulaire extension. If the errors are not validation errors for my fields, I re-throw the error, making Flarum render it in the error alert.

This PR appears to remove that feature. Now if errorHandler throws an exception, it would fail silently and not show any error alert.

I suppose any custom errorHandler could call app.alerts.show(error.alert);, but that was a nice feature, and would also show up if the error was unintentionally re-thrown from the errorHandler.

@clarkwinkelmann
Copy link
Member

clarkwinkelmann commented Feb 24, 2020

Would it make sense to log all errors to console, even if options.errorHandler is defined ?

We're already going to log most validation errors to the console with the current implementation I believe. We might as well log all request errors.

This would also simplify re-implementing the feature I said was removed in my previous comment as the conditions can be moved up in the code.

EDIT: I see this was discussed 10 days ago in one of the comment discussions above. If we don't want to log handled errors, maybe we should not log validation errors either ? This is kind of their expected handling if nothing more specific was defined 🤔

@dsevillamartin
Copy link
Member Author

You make a good point. I guess there's no reason not to log e.g. validation errors - it can make debugging easier even when it's just a handled error.

The catching of errorHandler might be a mistake on my part - if we agree, I can add the try/catch back and always log the error.

@franzliedke
Copy link
Contributor

Because we now auto-format our JS code with Prettier, this branch now has conflicts with master. Sorry about that. 😉

Please take the steps outlined in the forum to resolve the conflicts.

* Use decodeURI instead of unescape & don't close modals

* Add comment

* Don't use a try/catch, clean up the group log code

* Remove double negative

* Format; fix issues from rebasing
@dsevillamartin
Copy link
Member Author

I rebased & squashed as I realized I hadn't done it correctly.

I also restored the errorHandler default of throwing the error, and moved back to try/catch instead of if/else.

@clarkwinkelmann
Copy link
Member

Still looking good to me

@askvortsov1
Copy link
Sponsor Member

This doesn't clash with any of the state changes afaik, and we might as well put it in now instead of after TS when more work will be needed to get it updated. As 3 core devs have approved and there seems to be no conflicts, merging.

@askvortsov1 askvortsov1 merged commit 7e661df into master May 12, 2020
@askvortsov1 askvortsov1 deleted the ds/debug-modal-improvements branch May 12, 2020 16:23
@askvortsov1 askvortsov1 added this to the 0.1.0-beta.14 milestone May 12, 2020
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.

5 participants