-
Notifications
You must be signed in to change notification settings - Fork 668
chore: move exit code mapping to a central location #6411
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
Conversation
9d7664b to
1689497
Compare
1689497 to
a7199b0
Compare
| } | ||
|
|
||
| return defaultValue | ||
| } |
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.
suggestion: How about using a static map, this would be something we could easily migrate out to configuration at a later date:
// errorCatalogToExitCodeMap centralizes the relationship between catalog errors and CLI exit codes.
var errorCatalogToExitCodeMap = map[string]int{
code.NewUnsupportedProjectError("").ErrorCode: constants.SNYK_EXIT_CODE_UNSUPPORTED_PROJECTS,
// Add new mappings here
}
var MapErrorCatalogToExitCode func(err *snyk_errors.Error, defaultValue int) int = mapErrorToExitCode
func mapErrorToExitCode(err *snyk_errors.Error, defaultValue int) int {
if exitCode, internalExists := errorCatalogToExitCodeMap[err.ErrorCode]; internalExists {
return exitCode
}
return defaultValue
}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.
I discovered the Comma Ok pattern
Sometimes you need to distinguish a missing entry from a zero value. Is there an entry for "UTC" or is that 0 because it's not in the map at all? You can discriminate with a form of multiple assignment.
var seconds int var ok bool seconds, ok = timeZone[tz]For obvious reasons this is called the “comma ok” idiom. In this example, if tz is present, seconds will be set appropriately and ok will be true; if not, seconds will be set to zero and ok will be false. Here's a function that puts it together with a nice error report:
func offset(tz string) int { if seconds, ok := timeZone[tz]; ok { return seconds } log.Println("unknown time zone:", tz) return 0 }
Source: https://go.dev/doc/effective_go
a7199b0 to
8c905c7
Compare
1045017 to
6b450b9
Compare
7b471ec to
95d7175
Compare
a60a0ad to
eb7f0c4
Compare
d36963b to
3ad8cfb
Compare
3ad8cfb to
d8fed82
Compare
Pull Request Submission Checklist
are release-note ready, emphasizing
what was changed, not how.
What does this PR do?
This PR centralizes the mapping from errors to exit codes. This enables to consistently map errors no matter if they are part of the dataflow or returned from an extension.
Where should the reviewer start?
How should this be manually tested?
What's the product update that needs to be communicated to CLI users?
N/A
Risk assessment (Low | Medium | High)?
Medium, as it alters the flow of errors and exit codes. Looking into additional tests to reduce the risk more.