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

API (js) - handle non-2** (and 1**) responses as errors #783

Merged
merged 2 commits into from
Mar 24, 2017
Merged

API (js) - handle non-2** (and 1**) responses as errors #783

merged 2 commits into from
Mar 24, 2017

Conversation

himdel
Copy link
Contributor

@himdel himdel commented Mar 23, 2017

When API returns a 404 and you have code like..

API.get(...)
  .then(handleSuccess)
  .catch(handleError)

You'd expect handleError to be called.

But, until now, these would all call handleSuccess.

Fixing that :).

Also, updating miqService.handleFailure to console.error the error, so that we can see it in devel mode at least.

(A separate PR that makes handleFailure actually handle the failure and show a flash message will follow, that will need either a separate function or some more changes to be compatible with existing code. - so, after PTO :).)

Cc: @ZitaNemeckova , @mzazrivec

@himdel himdel added the bug label Mar 23, 2017
@ZitaNemeckova ZitaNemeckova mentioned this pull request Mar 23, 2017
11 tasks
@himdel himdel changed the title API (js) - handle 404s as errors API (js) - handle non-2** (and 1**) responses as errors Mar 23, 2017
if (e.message) {
$log.log(e.message);
console.error(e.message);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a reason to remove $log here.

How about using $log.error() instead of console.error (will also take care of the CC error)

Copy link
Contributor Author

@himdel himdel Mar 23, 2017

Choose a reason for hiding this comment

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

Looks like $log doesn't seem to work together with out toast-errors-in-devel-mode code :(.

And since we're not using $log anywhere else, I see no reason to fix that... do you?

EDIT: also, the point of CC warning about console.* is not to never use console.*, but to only use it in cases where console output is desired, as opposed to leaving debugging code in, or pretending to notify users by doing console.*. So... using $log instead of console just to prevent CC errors would be exactly the wrong solution. (And yes, we should add a CC warning about $log as well.)

@@ -158,6 +158,9 @@
if (response.status === 204) // No content
return null;

if (response.status >= 300) // Not 1** or 2**
return Promise.reject(response.json());

Copy link
Contributor

Choose a reason for hiding this comment

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

Curly braces for if please...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks! :)

when API returns a 404 and you have code like..

```
API.get(...)
  .then(handleSuccess)
  .catch(handleError)
```

You'd expect `handleError` to be called.

But, until now, these would all call `handleSuccess`.

Fixing that.

We'll try .json() on error anyway, since error messages from API are JSON. If the response is not json, it will throw anyway.
so that we at least see the error in devel mode and can add a proper error handler

handleFailure should really flash too, but.. that would be an incompatible change so leaving for a separate PR.
@miq-bot
Copy link
Member

miq-bot commented Mar 23, 2017

Checked commits https://github.com/himdel/manageiq-ui-classic/compare/fa897106f631e2167e7e3912f086a177a3be84f3~...bc8a622d652086bca2944ea90b8133f26e53bb9f with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
0 files checked, 0 offenses detected
Everything looks good. ⭐

@mzazrivec mzazrivec added this to the Sprint 57 Ending Mar 27, 2017 milestone Mar 24, 2017
@mzazrivec mzazrivec self-assigned this Mar 24, 2017
@mzazrivec mzazrivec merged commit a878599 into ManageIQ:master Mar 24, 2017
@himdel himdel deleted the api-404 branch March 24, 2017 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants