Adopt the native CommandKit application boundary
This commit is contained in:
49
internal/app/command_outcome.go
Normal file
49
internal/app/command_outcome.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
// inputError identifies an invalid Narratio request independently of native
|
||||
// configuration diagnostics. Operational errors retain their own classification.
|
||||
type inputError struct{ message string }
|
||||
|
||||
func (e *inputError) Error() string { return e.message }
|
||||
|
||||
func operationOutcome(err error) commandkit.Outcome {
|
||||
if err == nil {
|
||||
return commandkit.Success()
|
||||
}
|
||||
if classifyOperationError(err) == commandkit.OutcomeUsage {
|
||||
return commandkit.Usage(err.Error()).WithCause(err)
|
||||
}
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
}
|
||||
|
||||
func classifyOperationError(err error) commandkit.OutcomeKind {
|
||||
if _, ok := err.(*inputError); ok {
|
||||
return commandkit.OutcomeUsage
|
||||
}
|
||||
switch wrapped := err.(type) {
|
||||
case interface{ Unwrap() []error }:
|
||||
found := false
|
||||
for _, child := range wrapped.Unwrap() {
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
if classifyOperationError(child) != commandkit.OutcomeUsage {
|
||||
return commandkit.OutcomeOperational
|
||||
}
|
||||
}
|
||||
if found {
|
||||
return commandkit.OutcomeUsage
|
||||
}
|
||||
case interface{ Unwrap() error }:
|
||||
if child := wrapped.Unwrap(); child != nil {
|
||||
return classifyOperationError(child)
|
||||
}
|
||||
}
|
||||
return ckconfig.ClassifyConfigurationError(err)
|
||||
}
|
||||
Reference in New Issue
Block a user