2021-07-20 21:36:35 +03:00
|
|
|
# How to handle errors
|
|
|
|
|
|
|
|
```golang
|
2021-07-26 21:01:25 +03:00
|
|
|
import sce "github.com/ossf/scorecard/v2/errors"
|
2021-07-20 21:36:35 +03:00
|
|
|
|
|
|
|
// Public errors are defined in errors/public.go and are exposed to callers.
|
|
|
|
// Internal errors are defined in checks/errors.go. Their names start with errInternalXXX
|
|
|
|
|
|
|
|
// Examples:
|
|
|
|
|
|
|
|
// Return a standard check run failure, with an error message from an internal error.
|
|
|
|
// We only create internal errors for errors that may happen in several places in the code: this provides
|
|
|
|
// consistent error messages to the caller.
|
2021-07-21 19:02:43 +03:00
|
|
|
return sce.Create(sce.ErrScorecardInternal, errInternalInvalidYamlFile.Error())
|
2021-07-20 21:36:35 +03:00
|
|
|
|
|
|
|
// Return a standard check run failure, with an error message from an internal error and an API call error.
|
|
|
|
err := dependency.apiCall()
|
|
|
|
if err != nil {
|
2021-07-21 19:02:43 +03:00
|
|
|
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("%v: %v", errInternalSomething, err))
|
2021-07-20 21:36:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a standard check run failure, only with API call error. Use this format when there is no internal error associated
|
|
|
|
// to the failure. In many cases, we don't need internal errors.
|
|
|
|
err := dependency.apiCall()
|
|
|
|
if err != nil {
|
2021-07-21 19:02:43 +03:00
|
|
|
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("dependency.apiCall: %v", err))
|
2021-07-20 21:36:35 +03:00
|
|
|
}
|
2021-07-26 21:01:25 +03:00
|
|
|
```
|