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

Error codes normalization #1481

Merged
merged 29 commits into from
Oct 1, 2019
Merged

Error codes normalization #1481

merged 29 commits into from
Oct 1, 2019

Conversation

scottinet
Copy link
Contributor

@scottinet scottinet commented Sep 26, 2019

Can be merged, but do not release Kuzzle until kuzzleio/kuzzle-common-objects#84 is also merged, and this repository's dependencies updated accordingly

Description

Errors normalization is back.

Same as for the v1 (#1478 ), but updated for Kuzzle v2.

For a complete list of changes about error codes, check the other PR. Here I'll detail the changes specific to Kuzzle v2:

  • Replaced the baseController.tryGetBoolean and the baseController.getXxxParam methods, because error messages thrown couldn't be protocol agnostic (this could be confusing for users, especially for those using HTTP). Also, the previous methods used property paths, which is slower than accessing directly to a property (until now, we don't need paths). The new methods now are:
    • getBody<Type<(request, name, default): gets a body property. Handled types: object, array, string, number, integer, boolean ("integer" is new and enforce an integer number)
    • getArg(request, name, default): gets an optional property (in request.input.args). Handled types: object, array, string, number, integer, boolean ("integer" is new and enforce an integer number)
    • getBody(request, default): throws a dedicated api.assert.body_required error if the request doesn't have a body, unless a default value is provided
  • The error codes generator now skips deprecated errors, subdomains and/or domains
  • Updated the migation documentation with the new error codes

How to review

Since #1478 has been merged, this review should only be about the changes made in the BaseController class, and the updated v2 documentation.

'services',
'storage',
'incomplete_delete',
errors));
}

// @todo should be done in clientAdapter ?
Copy link
Contributor

Choose a reason for hiding this comment

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

INFO Complete the task associated to this TODO comment. rule

@@ -1438,9 +1436,7 @@ class ElasticSearch extends Service {

// @todo should be done in clientAdapter
Copy link
Contributor

Choose a reason for hiding this comment

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

INFO Complete the task associated to this TODO comment. rule

@codecov
Copy link

codecov bot commented Sep 27, 2019

Codecov Report

Merging #1481 into 2-dev will increase coverage by 0.01%.
The diff coverage is 91.93%.

Impacted file tree graph

@@            Coverage Diff             @@
##            2-dev    #1481      +/-   ##
==========================================
+ Coverage   92.63%   92.64%   +0.01%     
==========================================
  Files          98       98              
  Lines        6582     6637      +55     
==========================================
+ Hits         6097     6149      +52     
- Misses        485      488       +3
Impacted Files Coverage Δ
lib/api/core/models/security/user.js 100% <100%> (ø) ⬆️
lib/api/core/plugins/pluginManifest.js 100% <100%> (ø) ⬆️
...api/core/entrypoints/service/httpFormDataStream.js 100% <100%> (ø) ⬆️
lib/api/core/models/repositories/repository.js 97.69% <100%> (-0.11%) ⬇️
lib/api/core/validation/types/object.js 100% <100%> (ø) ⬆️
lib/api/controllers/serverController.js 94.11% <100%> (+0.08%) ⬆️
lib/api/core/entrypoints/protocols/mqtt.js 95.34% <100%> (ø) ⬆️
lib/api/core/sdk/funnelProtocol.js 100% <100%> (ø) ⬆️
lib/api/core/httpRouter/index.js 98.66% <100%> (ø) ⬆️
lib/api/core/entrypoints/protocols/websocket.js 96.96% <100%> (ø) ⬆️
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f33baa9...9e8c608. Read the comment docs.

const
flagName = flagPath.split('.').slice(-1),
flagValue = _.get(request, `input.${flagPath}`);
const flagValue = _.get(request, `input.${flagPath}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we stop using path in this method too. We only use this method to extract notify and includeKuzzleMeta from input.args

Copy link
Contributor Author

@scottinet scottinet Oct 1, 2019

Choose a reason for hiding this comment

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

This review seems to have been made on an obsolete version of that branch: the method tryGetBoolean was removed in the optimization commit

lib/api/controllers/baseController.js Outdated Show resolved Hide resolved
assertionError.throw('missing_argument', errorName);
}

value = Number.parseFloat(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't be parseInt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

parseInt truncates numbers to transform them into integers:

> Number.parseInt('3.14')
3

We don't want to change provided values silently, so we need to parse the number as a floating-point value, and then verify that it's an integer number.

getBodyNumber (request, name, def = null) {
const body = request.input.body;

if (body === null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use getBody method here and in the other methods ?

Copy link
Contributor Author

@scottinet scottinet Oct 1, 2019

Choose a reason for hiding this comment

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

For the same reason we made separate methods for these getters: to use getBody, we'd need to use it without a default value, otherwise it would return different types, triggering a de-opt, cascaded to callers.

So, we have a choice between (current):

const body = request.input.body;

if (body === null) {
  if (def !== null) {
    return def;
  }

  assertionError.throw('body_required');
}

And:

let body;

try {
  body = getBody(request);
}
catch (e) {
  if (def !== null) {
    return def;
  }

  throw e;
}

Which is longer and more complicated.

Co-Authored-By: Adrien Maret <amaret93@gmail.com>
@kuzzle
Copy link
Contributor

kuzzle commented Oct 1, 2019

SonarQube analysis reported 11 issues

  • INFO 11 info

Watch the comments in this conversation to review them.

9 extra issues

Note: The following issues were found on lines that were not modified in the pull request. Because these issues can't be reported as line comments, they are summarized here:

  1. INFO adminController.js#L57: Complete the task associated to this TODO comment. rule
  2. INFO baseController.js#L635: Complete the task associated to this TODO comment. rule
  3. INFO documentController.js#L491: Complete the task associated to this TODO comment. rule
  4. INFO repository.js#L69: Complete the task associated to this TODO comment. rule
  5. INFO tokenRepository.js#L219: Complete the task associated to this TODO comment. rule
  6. INFO pluginContext.js#L99: Complete the task associated to this TODO comment. rule
  7. INFO pluginsManager.js#L904: Complete the task associated to this TODO comment. rule
  8. INFO elasticsearch.js#L1469: Complete the task associated to this TODO comment. rule
  9. INFO elasticsearch.js#L1529: Complete the task associated to this TODO comment. rule

@scottinet scottinet requested a review from Aschen October 1, 2019 08:00
@Aschen Aschen merged commit f7764d8 into 2-dev Oct 1, 2019
@Aschen Aschen deleted the fix-error-codes-v2 branch October 1, 2019 12:14
@Aschen Aschen mentioned this pull request Nov 5, 2019
Aschen added a commit that referenced this pull request Nov 5, 2019
# [2.0.0-rc.2](https://github.com/kuzzleio/kuzzle/releases/tag/2.0.0-rc.2) (2019-11-05)


#### Breaking changes

- [ [#1500](#1500) ] Remove Dsl constructor from plugin context   ([Aschen](https://github.com/Aschen))
- [ [#1489](#1489) ] Drop support for socket.io   ([scottinet](https://github.com/scottinet))
- [ [#1491](#1491) ] Remove strategy constructors support   ([Aschen](https://github.com/Aschen))
- [ [#1476](#1476) ] Change Document controller m* routes returns & fix SDK functional tests   ([Aschen](https://github.com/Aschen))

#### Bug fixes

- [ [#1512](#1512) ] Fix subscribing with a dead connection   ([benoitvidis](https://github.com/benoitvidis))
- [ [#1507](#1507) ] Properly destroy a user after a credentials creation failure   ([scottinet](https://github.com/scottinet))
- [ [#1483](#1483) ] Fix socketio connections leak & deactivate this protocol by default   ([scottinet](https://github.com/scottinet))

#### New features

- [ [#1502](#1502) ] Add a new "document:exists" API route   ([scottinet](https://github.com/scottinet))
- [ [#1501](#1501) ] Add collection delete   ([Aschen](https://github.com/Aschen))
- [ [#1499](#1499) ] Expose an Elasticsearch client constructor in plugin context   ([Aschen](https://github.com/Aschen))
- [ [#1484](#1484) ] Upgrade script   ([scottinet](https://github.com/scottinet))

#### Enhancements

- [ [#1510](#1510) ] Mapping - enable dynamic templates   ([benoitvidis](https://github.com/benoitvidis))
- [ [#1495](#1495) ] Check for SDK compatibility against current Kuzzle version   ([Aschen](https://github.com/Aschen))
- [ [#1498](#1498) ] Add highlight to search results   ([Aschen](https://github.com/Aschen))
- [ [#1488](#1488) ] Add kuzzle version to the dumped informations   ([scottinet](https://github.com/scottinet))
- [ [#1481](#1481) ] Error codes normalization   ([scottinet](https://github.com/scottinet))
- [ [#1478](#1478) ] Error codes redux   ([scottinet](https://github.com/scottinet))
---
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants