-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add oclif pretty printer #27
Changes from all commits
a962061
cc0dd61
ebb6613
b55c9c6
ce5920d
fb49407
e8d0e06
bc60be5
36bdab9
a558837
51098c4
ce0c6de
7ea63a7
fb4718a
b0672bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import * as wrap from 'wrap-ansi' | ||
| import indent = require('indent-string') | ||
| import * as screen from '../screen' | ||
| import {config} from '../config' | ||
|
|
||
| export interface PrettyPrintableError { | ||
| /** | ||
| * messsage to display related to the error | ||
| */ | ||
| message?: string; | ||
|
|
||
| /** | ||
| * a unique error code for this error class | ||
| */ | ||
| code?: string; | ||
|
|
||
| /** | ||
| * a url to find out more information related to this error | ||
| * or fixing the error | ||
| */ | ||
| ref?: string; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
|
|
||
| /** | ||
| * a suggestion that may be useful or provide additional context | ||
| */ | ||
| suggestion?: string; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have found that we typically want multiple suggestions. https://github.com/forcedotcom/cli-packages/blob/develop/packages/command/src/sfdxCommand.ts#L503 |
||
| } | ||
|
|
||
| // These exist for backwards compatibility with CLIError | ||
| type CLIErrorDisplayOptions = { name?: string; bang?: string } | ||
|
|
||
| export function applyPrettyPrintOptions(error: Error, options: PrettyPrintableError): PrettyPrintableError { | ||
| const prettyErrorKeys: (keyof PrettyPrintableError)[] = ['message', 'code', 'ref', 'suggestion'] | ||
|
|
||
| prettyErrorKeys.forEach(key => { | ||
| const applyOptionsKey = !(key in error) && options[key] | ||
| if (applyOptionsKey) { | ||
| (error as PrettyPrintableError)[key] = options[key] | ||
| } | ||
| }) | ||
|
|
||
| return error | ||
| } | ||
|
|
||
| export default function prettyPrint(error: Error & PrettyPrintableError & CLIErrorDisplayOptions) { | ||
| if (config.debug) { | ||
| return error.stack | ||
| } | ||
|
|
||
| const {message, code, suggestion, ref, name: errorSuffix, bang} = error | ||
|
|
||
| // errorSuffix is pulled from the 'name' property on CLIError | ||
| // and is like either Error or Warning | ||
| const formattedHeader = message ? `${errorSuffix || 'Error'}: ${message}` : undefined | ||
| const formattedCode = code ? `Code: ${code}` : undefined | ||
| const formattedSuggestion = suggestion ? `Suggestion: ${suggestion}` : undefined | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Salesforce CLI says "Try this:". I like that a little more than "Suggestion:" but I don't care all that much. It might be worth it to have doc weigh-in. Or I guess to say it another way, if the Salesforce CLI would ever want to use this standard oclif error format, we would want to run it by doc. |
||
| const formattedReference = ref ? `Reference: ${ref}` : undefined | ||
|
|
||
| const formatted = [formattedHeader, formattedCode, formattedSuggestion, formattedReference] | ||
| .filter(Boolean) | ||
| .join('\n') | ||
|
|
||
| let output = wrap(formatted, screen.errtermwidth - 6, {trim: false, hard: true} as any) | ||
| output = indent(output, 3) | ||
| output = indent(output, 1, {indent: bang || '', includeEmptyLines: true} as any) | ||
| output = indent(output, 1) | ||
|
|
||
| return output | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this is in an
oclifproperty but the other things likeref,codeetc are on the actual error object? I always thought the exitCode should be on the error but a personal opinion.In any case, this is our error object. Some stuff is specific to Salesforce like labels, but having the properties like actions vs. suggestion would be nice.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was more for backwards compatibility with the existing structure of cli error and the
oclifproperty that was defined prior. Then thehandlefunction expects it. It's unlikely people would have a different version of the@oclif/errorsor would be creating their own errors withoclif.exitbut we didn't want to risk it. Instead we wanted to call out it as an oclif error concern one that isn't involved with the display of the error. With yourexitCodeare you using a customhandlefunction inbin/run, too?