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 stacktrace overhaul #1944

Merged
merged 14 commits into from
Jan 29, 2021
Merged

Error stacktrace overhaul #1944

merged 14 commits into from
Jan 29, 2021

Conversation

Aschen
Copy link
Contributor

@Aschen Aschen commented Jan 27, 2021

What does this PR do ?

Highlight user code in internal stacktrace.

How should this be manually tested?

The following code will generate the following stacktrace:

const { Backend, BadRequestError } = require('../../kuzzle/index');

const app = new Backend('foobar');

async function apiError () {
  throw new BadRequestError('houston we have a problem');
}

async function jsError () {
  houstonWeHaveAProblem();
}

app.controller.register('stack', {
  actions: {
    apiError: {
      handler: async () => {
        await apiError();
      }
    },
    jsError: {
      handler: async () => {
        await jsError();
      }
    },
    sdkError: {
      handler: async () => {
        await app.sdk.query({
          controller: 'stack',
          action: 'apiError'
        });
      }
    }
  }
})

app.start()

stack:apiError

BadRequestError: houston we have a problem
       at new BadRequestError (/home/aschen/projets/kuzzleio/kuzzle/lib/kerror/errors/badRequestError.ts:26:5)
>    at apiError (/home/aschen/projets/kuzzleio/lp/backend/test.js:6:9)
>    at BaseController.handler [as apiError] (/home/aschen/projets/kuzzleio/lp/backend/test.js:17:15)
       at doAction (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:759:47)
       at Funnel.processRequest (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:423:34)

stack:jsError

ReferenceError: houstonWeHaveAProblem is not defined
>    at jsError (/home/aschen/projets/kuzzleio/lp/backend/test.js:10:3)
>    at BaseController.handler (/home/aschen/projets/kuzzleio/lp/backend/test.js:22:15)
       at doAction (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:759:47)
       at Funnel.processRequest (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:423:34)

stack:sdkError

BadRequestError: houston we have a problem
          at new BadRequestError (/home/aschen/projets/kuzzleio/kuzzle/lib/kerror/errors/badRequestError.ts:26:5)
>    at apiError (/home/aschen/projets/kuzzleio/laposte-caisse-mobiles/backend/test.js:6:9)
>    at BaseController.handler [as apiError] (/home/aschen/projets/kuzzleio/lp/backend/test.js:17:15)
          at doAction (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:759:47)
          at Funnel.executePluginRequest (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:502:20)
          at FunnelProtocol.query (/home/aschen/projets/kuzzleio/kuzzle/lib/core/shared/sdk/funnelProtocol.js:74:49)
>    at BaseController.handler [as sdkError] (/home/aschen/projets/kuzzleio/lp/backend/test.js:27:9)
          at Funnel.processRequest (/home/aschen/projets/kuzzleio/kuzzle/lib/api/funnel.js:423:28)

Other Changes

Boyscout

  • export Global in a TS file to avoid the necessity of building the app to run it

@Aschen Aschen marked this pull request as ready for review January 28, 2021 08:07
@codecov
Copy link

codecov bot commented Jan 28, 2021

Codecov Report

Merging #1944 (9c564e6) into 2-dev (7231538) will increase coverage by 0.01%.
The diff coverage is 87.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##            2-dev    #1944      +/-   ##
==========================================
+ Coverage   93.76%   93.77%   +0.01%     
==========================================
  Files         120      121       +1     
  Lines        7572     7604      +32     
==========================================
+ Hits         7100     7131      +31     
- Misses        472      473       +1     
Impacted Files Coverage Δ
lib/kuzzle/kuzzle.js 76.59% <0.00%> (ø)
lib/core/shared/sdk/funnelProtocol.js 86.95% <33.33%> (-0.55%) ⬇️
lib/core/network/protocols/mqtt.js 96.77% <100.00%> (ø)
lib/core/network/removeErrorStack.js 100.00% <100.00%> (ø)
lib/kerror/index.js 95.83% <100.00%> (+0.08%) ⬆️
lib/service/storage/elasticsearch.js 92.21% <100.00%> (ø)
lib/service/storage/esWrapper.js 46.37% <100.00%> (ø)
lib/util/deprecate.js 88.46% <100.00%> (ø)
lib/util/didYouMean.js 87.50% <100.00%> (-12.50%) ⬇️
lib/util/stackTrace.js 100.00% <100.00%> (ø)
... and 2 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 7231538...9c564e6. Read the comment docs.

Copy link
Contributor

@Leodau Leodau left a comment

Choose a reason for hiding this comment

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

Make sense to bring NODE_ENV into our global object 👌

doc/2/guides/introduction/general-purpose-backend/index.md Outdated Show resolved Hide resolved
@sonarcloud
Copy link

sonarcloud bot commented Jan 28, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Leodau Leodau linked an issue Jan 28, 2021 that may be closed by this pull request
|| (line.indexOf('at /') === -1 && line.charAt(line.indexOf('(') + 1) !== '/') // ignore node internal
|| line.includes('node_modules') // ignore dependencies
) {
return ' ' + line;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we include the line if the comments say it is ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's an internal line from Node.js or from Kuzzle so we keep it either way because it's a bad pratice to hide stack calls (for us it's more difficult to debug Kuzzle bug, for users it can lead to confusion)

* > at init (/home/aschen/projets/app/test.ts:8:3)
* at Module._compile (internal/modules/cjs/loader.js:1133:30)
*/
function hilightUserCode (line) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm wrong but, rather than highlighting user code, this function filters out system code. #renaming #nitpicking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nop, it does not remove any line from the stacktrace

else if (data && data.content && data.content.error) {
delete data.content.error.stack;
else {
data.content.error.stack = data.content.error.stack.split('\n').map(hilightUserCode).join('\n');
Copy link
Contributor

@Shiranuit Shiranuit Jan 29, 2021

Choose a reason for hiding this comment

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

      data.content.error.stack = data.content.error.stack.split('\n').map(hilightUserCode).join('\n');

Maybe having a function to do this would be better instead of repeating this bit of code everywhere ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't because I use this method in a custom way in the kerror/index.js file

@Aschen Aschen merged commit eb69701 into 2-dev Jan 29, 2021
@Aschen Aschen deleted the error-stacktrace-overhaul branch January 29, 2021 09:02
@Aschen Aschen mentioned this pull request Jan 29, 2021
Aschen added a commit that referenced this pull request Jan 29, 2021
# [2.9.2](https://github.com/kuzzleio/kuzzle/releases/tag/2.9.2) (2021-01-29)


#### Bug fixes

- [ [#1938](#1938) ] Export BackendCluster class   ([Aschen](https://github.com/Aschen))

#### Enhancements

- [ [#1944](#1944) ] Error stacktrace overhaul   ([Aschen](https://github.com/Aschen))
- [ [#1946](#1946) ] Allows to not send realtime notification on document controller   ([Aschen](https://github.com/Aschen))
---
Aschen pushed a commit that referenced this pull request Apr 11, 2022
## What does this PR do ?

Work had been done on stacktrace a while ago to enhance them so developers can distinguish there code from Kuzzle one. (See #1944)

Few problems remains:
 - `hilightUserCode` was called many times on the same stack
 - errors thrown by the SDK in a pipe did not include the line with the code that triggered the error

This fix cover the following additional use cases:

```
app.pipe.register('server:afterNow', async req => {
  await app.sdk.collection.list('do-not-exist');
});

app.pipe.register('server:afterInfo', async req => {
  throw new BadRequestError('afterInfo');
});

app.pipe.register('server:afterGetStats', async function afterGetStats (req) {
  await app.sdk.collection.list('do-not-exist');
});

app.pipe.register('server:afterMetrics', async function afterMetrics (req) {
  throw new BadRequestError('afterMetrics');
});
```

### Other changes
 - remove useless await in FunnelProtocol + convert to Typescript
 - extract `removeErrorStack` into `util` directory
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.

Error stacktrace overhaul
4 participants