Skip to content

Commit

Permalink
refactor errors (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven Harrison authored Feb 5, 2024
1 parent e17f870 commit e4d777b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
102 changes: 54 additions & 48 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,86 +11,92 @@ type ReturnError struct {
Code int
}

const generalExecutionErr = 100

func getErrInvalidFlags(err error) *ReturnError {
return &ReturnError{
error: err,
Code: 101,
}
return getError(
err,
101,
)
}

func getErrFailedToLoadSpec(what string, source *load.Source, err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("failed to load %s spec from %s with %v", what, source.Out(), err),
Code: 102,
}
return getError(
fmt.Errorf("failed to load %s spec from %s with %v", what, source.Out(), err),
102,
)
}

func getErrFailedToLoadSpecs(what string, path string, err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("failed to load %s specs from glob %q with %v", what, path, err),
Code: 103,
}
return getError(
fmt.Errorf("failed to load %s specs from glob %q with %v", what, path, err),
103,
)
}

func getErrDiffFailed(err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("diff failed with %v", err),
Code: 104,
}
return getError(
fmt.Errorf("diff failed with %v", err),
104,
)
}

func getErrFailedPrint(what string, err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("failed to print %q with %v", what, err),
Code: 105,
}
return getError(
fmt.Errorf("failed to print %q with %v", what, err),
105,
)
}

func getErrUnsupportedDiffFormat(format string) *ReturnError {
return &ReturnError{
error: fmt.Errorf("format %q is not supported by \"diff\"", format),
Code: 109,
}
return getError(
fmt.Errorf("format %q is not supported by \"diff\"", format),
109,
)
}

func getErrUnsupportedSummaryFormat(format string) *ReturnError {
return &ReturnError{
error: fmt.Errorf("format %q is not supported by \"summary\"", format),
Code: 110,
}
return getError(
fmt.Errorf("format %q is not supported by \"summary\"", format),
110,
)
}

func getErrUnsupportedChangelogFormat(format string) *ReturnError {
return &ReturnError{
error: fmt.Errorf("format %q is not supported by \"changelog\"", format),
Code: 111,
}
return getError(
fmt.Errorf("format %q is not supported by \"changelog\"", format),
111,
)
}

func getErrUnsupportedBreakingChangesFormat(format string) *ReturnError {
return &ReturnError{
error: fmt.Errorf("format %q is not supported by \"breaking\"", format),
Code: 112,
}
return getError(
fmt.Errorf("format %q is not supported by \"breaking\"", format),
112,
)
}

func getErrUnsupportedChecksFormat(format string) *ReturnError {
return &ReturnError{
error: fmt.Errorf("format %q is not supported with \"checks\"", format),
Code: 113,
}
return getError(
fmt.Errorf("format %q is not supported with \"checks\"", format),
113,
)
}

func getErrInvalidColorMode(err error) *ReturnError {
return &ReturnError{
error: err,
Code: 114,
}
return getError(
err,
114,
)
}

func getErrCantProcessIgnoreFile(what string, err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("can't process %s ignore file %v", what, err),
Code: 121,
}
return getError(
fmt.Errorf("can't process %s ignore file %v", what, err),
121,
)
}

func getError(err error, code int) *ReturnError {
return &ReturnError{err, code}
}
2 changes: 1 addition & 1 deletion internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func run(cmd *cobra.Command) int {
if ret := getReturnValue(cmd); ret != 0 {
return ret
}
return 100
return generalExecutionErr
}

return getReturnValue(cmd)
Expand Down

0 comments on commit e4d777b

Please sign in to comment.