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

fix(rest): log unexpected errors to console #1058

Merged
merged 2 commits into from Mar 1, 2018
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Feb 27, 2018

This is a follow-up and a partial revert of #939.

The first commit adds a check to verify that no console logs are produced by our tests, beyond what Mocha prints. This is needed to ensure clean test output in the future.

The second commit reverts the default implementation of LogError sequence action to use console.error again (instead of debug) and fixes tests to suppress error messages for requests that fail with the expected error.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Related API Documentation was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in packages/example-* were updated

@bajtos bajtos added bug developer-experience Issues affecting ease of use and overall experience of LB users labels Feb 27, 2018
@bajtos bajtos added this to the February 2018 milestone Feb 27, 2018
@bajtos bajtos self-assigned this Feb 27, 2018
@@ -37,6 +37,12 @@ function run(argv, dryRun) {
);
mochaOpts.unshift('--require', sourceMapRegisterPath);
}

// Fail any tests that are printing to console.
mochaOpts.unshift(
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest that we add --allow-console-logs option (so that @loopback/build can be used by other modules outside of LoopBack 4).

  1. --allow-console-logs is default to false if not present
  2. --allow-console-logs can be used to override our check

Copy link
Member Author

Choose a reason for hiding this comment

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

To be honest, I think this is YAGNI. IMO, every developer out there should have a test suite that's not printing random stuff to console output. If somebody comes to us complaining about this particular feature of lb-mocha, only then it is the right time to implement the flag. It will also allow us to learn that somebody is actually using @loopback/build.

Copy link
Contributor

Choose a reason for hiding this comment

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

Our CLI allows generated loopback apps to use @loopback/build.

Since it's trivial to add this option to be consistent with other wrappers, I would moderately prefer to just add it now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough.

@@ -6,20 +6,18 @@
import {Provider} from '@loopback/context';
import {ServerRequest} from 'http';
import {LogError} from '../internal-types';
import * as debugModule from 'debug';
const debug = debugModule('loopback:rest:error');

export class LogErrorProvider implements Provider<LogError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to have a provider.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add it as an optional dependency of the DefaultSequence?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is already a required dependency of the DefaultSequence. Changing it to an optional dependency is out of scope of this pull request IMO. Please note that I am not changing the design of LogError sequence action in this patch, just the implementation details.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Errors are logged by the reject action. The LogError function is injected into our default implementation of reject, see

https://github.com/strongloop/loopback-next/blob/45a4bdf2ccd0a92e647ff78acea730442d78abde/packages/rest/src/providers/reject.ts#L13-L17

This allows us to keep the sequence as a high-level description of steps to perform, and let individual sequence actions to deal with lower-level concerns like error handling and logging.

https://github.com/strongloop/loopback-next/blob/45a4bdf2ccd0a92e647ff78acea730442d78abde/packages/rest/src/sequence.ts#L105-L114

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

Good stuff. Please address my comments.

Modify the command "lb-mocha" to load a helper file that detects
calls of console.log/console.error/console.warn APIs and fails
the test suite in such case.

This forces developers to write tests in such way that the console
output remain clean, uncluttered by random logs.
Revert back the recent change that moved error logs from console
to debug logs.

With this commit in place, when a request handler fails with a 5xx
error, this error is printed via `console.error()` to notify people
operating the application about a possible problem.
@bajtos
Copy link
Member Author

bajtos commented Mar 1, 2018

@raymondfeng I added support for --allow-console-logs as you requested, see https://github.com/strongloop/loopback-next/pull/1058/files#diff-1305bb9d357da10fe6182a0351613503R41.

LGTY now?

@bajtos bajtos requested a review from raymondfeng March 1, 2018 08:32
@bajtos bajtos modified the milestones: February 2018, March 2018 Mar 1, 2018
@raymondfeng raymondfeng merged commit b7b0fd8 into master Mar 1, 2018
raymondfeng pushed a commit that referenced this pull request Mar 1, 2018
Modify the command "lb-mocha" to load a helper file that detects
calls of console.log/console.error/console.warn APIs and fails
the test suite in such case.

This forces developers to write tests in such way that the console
output remain clean, uncluttered by random logs.
@raymondfeng raymondfeng deleted the improve/logging branch March 1, 2018 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug developer-experience Issues affecting ease of use and overall experience of LB users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants