Skip to content

Print extension errors to the console - #86405

Closed
Konstantin Solomatov (solomatov) wants to merge 6 commits into
microsoft:masterfrom
solomatov:better_ext_errors
Closed

Konstantin Solomatov (solomatov) wants to merge 6 commits into
microsoft:masterfrom
solomatov:better_ext_errors

Conversation

@solomatov

Copy link
Copy Markdown
Contributor

Johannes Rieken (@jrieken) That's another attempt to improve diagnostics.

@solomatov

Copy link
Copy Markdown
Contributor Author

So, the problem I am trying to solve with this, is visibility of extension errors. They are printed to some log file, but navigating there takes time, and there's no possibility of navigating with Cmd+Click to the source from there.

@solomatov

Copy link
Copy Markdown
Contributor Author

See this: #86109

@bpasero

Copy link
Copy Markdown
Contributor

Will check in December. Have to figure out who is registering external errors.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) do you mean January?

@bpasero Benjamin Pasero (bpasero) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not feel like the "external error" part is something of my area, it is the first time I see it and it seems only to be used from many editor contributions that ask provider for results. Thus, assigning Johannes Rieken (@jrieken) and alexandrudima to decide if this is correct thing to do.

However, as for where to hook this into the workbench, it should be here:

setUnexpectedErrorHandler(error => this.handleUnexpectedError(error, logService));

@jrieken
Johannes Rieken (jrieken) removed their request for review December 6, 2019 14:41
@jrieken

Copy link
Copy Markdown
Contributor

"External error" are those that are caused by extensions and because we don't want them in our error telemetry we have special handling for them. That's the fragment I know of this and the important part to keep in mind when looking at the errors-util - of which I am not very knowledgeable.

@alexdima Alexandru Dima (alexdima) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I would log even VS Code internal errors to console.log... I am not sure why we are so picky today and log errors only to the logfile.

@bpasero

Copy link
Copy Markdown
Contributor

alexandrudima but we do, no? That is what:

setUnexpectedErrorHandler(error => this.handleUnexpectedError(error, logService));

essentially does.

@alexdima

Copy link
Copy Markdown
Member

Benjamin Pasero (@bpasero) I looked at

private handleUnexpectedError(error: unknown, logService: ILogService): void {
const message = toErrorMessage(error, true);
if (!message) {
return;
}
const now = Date.now();
if (message === this.previousUnexpectedError.message && now - this.previousUnexpectedError.time <= 1000) {
return; // Return if error message identical to previous and shorter than 1 second
}
this.previousUnexpectedError.time = now;
this.previousUnexpectedError.message = message;
// Log it
logService.error(message);
}
and that appears to send it only to the log service.

AFAICT, this PR adds that errors that occur in extension are additionally being logged to the console. My review was that I would print all errors to the console, internal ones and extension ones.

@bpasero

Benjamin Pasero (bpasero) commented Dec 6, 2019 •

Copy link
Copy Markdown
Contributor

alexandrudima my understanding is that we always have a console logger running that puts everything to console as well:

export class ConsoleLogService extends AbstractLogService implements ILogService {

Here is an error I logged via log service:

image

Maybe Sandeep Somavarapu (@sandy081) could chime in on that.

If this is true, the PR becomes easier, because the same method can be used basically for internal and external errors.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) alexandrudima Johannes Rieken (@jrieken) So is there anything I can do with PR?

@bpasero

Copy link
Copy Markdown
Contributor

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) Done. Made the PR much simpler :-)

@bpasero

Copy link
Copy Markdown
Contributor

Konstantin Solomatov (@solomatov) I am not sure this resolves the issue of reporting external errors, or does it? I think your other changes for allowing to listen on external errors is still needed, no?

As for printing errors to the console, this should happen automatically when using the log service as I outlined before.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) It doesn't happen automatically. It's being prints only to the log which is available in the debugged copy of vscode.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) So ok. We write the fact that the error message happened. But, we don't write a stacktrace. I think it's better to write it in a way which will print exception there and remove my change.

@bpasero

Copy link
Copy Markdown
Contributor

Yeah but then I would say that we should do that at the place where we log to the console, no? That way everyone benefits. Let's wait for Sandeep Somavarapu (@sandy081) to chime in. To summarize: every error that gets logged to the logger should be printed to the dev tools with a stack (if its an error).

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) It was printed but in a not very helpful way. See my last commit. It became even better :-)

@solomatov

Copy link
Copy Markdown
Contributor Author

I think, it was a bug. We didn't consider Errors without a message or with an empty or undefined messages as real errors.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) So, the problem was, I put a number of exceptions without error message, created like this: throw new Error(). These errors didn't print out because they didn't have message. My last commit fixes this problem. Such errors are now printed like everything else.

@bpasero

Copy link
Copy Markdown
Contributor

Konstantin Solomatov (@solomatov) ah nice, I like where this is going. However, one suggestion: if exception.message is not defined, let's simply use the generic error message An unknown error occurred. Please consult the log for more details. so that we make sure that even in the verbose case, no undefined message is printed together with the stack. Does that make sense?

@solomatov

Konstantin Solomatov (solomatov) commented Dec 7, 2019 •

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) I debugged it. Actually, it's defined and it's an empty string but due to how conditions works in js, it has funny behavior. Added your suggestions.

@sandy081

Copy link
Copy Markdown
Member

Yes, we log all messages to both log file and console.

Konstantin Solomatov (@solomatov)

We write the fact that the error message happened. But, we don't write a stacktrace. I think it's better to write it in a way which will print exception there and remove my change.

Yes, Console logger is not handling errors correctly thereby not printing stack traces whereas file logger (spdlog) does it correctly. Feel free to fix the console logger

error(message: string, ...args: any[]): void {
if (this.getLevel() <= LogLevel.Error) {
console.log('%c ERR', 'color: #f33', message, ...args);
}
}
critical(message: string, ...args: any[]): void {
if (this.getLevel() <= LogLevel.Critical) {
console.log('%cCRITI', 'background: #f33; color: white', message, ...args);
}
}

You can refer to code how we do it already in spdlogger

error(message: string | Error, ...args: any[]): void {
if (this.getLevel() <= LogLevel.Error) {
if (message instanceof Error) {
const array = Array.prototype.slice.call(arguments) as any[];
array[0] = message.stack;
this._log(LogLevel.Error, this.format(array));
} else {
this._log(LogLevel.Error, this.format([message, ...args]));
}
}
}

@solomatov

Copy link
Copy Markdown
Contributor Author

Sandeep Somavarapu (@sandy081) So, do you want me to use the same code for printing exception? Or is my way is ok? (I think they are more or less equivalent.)

@sandy081

Copy link
Copy Markdown
Member

I would prefer the existing way as it was there already (which is in spdlog).

@solomatov

Copy link
Copy Markdown
Contributor Author

Sandeep Somavarapu (@sandy081) I checked it, and Error isn't passed there. Just a string sent there.

@solomatov

Copy link
Copy Markdown
Contributor Author

Benjamin Pasero (@bpasero) Sandeep Somavarapu (@sandy081) Created #86617 to make the change. Closing this PR.

@solomatov
Konstantin Solomatov (solomatov) deleted the better_ext_errors branch December 9, 2019 20:07
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants