92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package app
|
|
|
|
import (
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"go.maximumdirect.net/ckconfig"
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
// inputError identifies an invalid Narratio request independently of native
|
|
// configuration diagnostics. Operational errors retain their own classification.
|
|
type inputError struct {
|
|
message string
|
|
cause error
|
|
}
|
|
|
|
func (e *inputError) Unwrap() error { return e.cause }
|
|
|
|
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 {
|
|
switch err.(type) {
|
|
case *inputError, *config.ArtifactCatalogInputError:
|
|
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)
|
|
}
|
|
|
|
// reportOperationOutcome leaves native configuration evidence to ckconfig while
|
|
// retaining the operation's context and any joined cleanup or persistence causes.
|
|
func reportOperationOutcome(inv commandkit.Invocation, err error) commandkit.Outcome {
|
|
outcome := operationOutcome(err)
|
|
var details []commandkit.Detail
|
|
var visit func(error)
|
|
visit = func(e error) {
|
|
if e == nil {
|
|
return
|
|
}
|
|
if load, ok := e.(*configkit.LoadError); ok {
|
|
warnings, explanation := ckconfig.AdaptDiagnostics("Configuration:\n", load.Sources(), load.Diagnostics())
|
|
for _, warning := range warnings {
|
|
_ = inv.Reporter().Warn("", warning.Write)
|
|
}
|
|
details = append(details, explanation.Details...)
|
|
return
|
|
}
|
|
switch wrapped := e.(type) {
|
|
case interface{ Unwrap() []error }:
|
|
for _, child := range wrapped.Unwrap() {
|
|
visit(child)
|
|
}
|
|
case interface{ Unwrap() error }:
|
|
visit(wrapped.Unwrap())
|
|
}
|
|
}
|
|
visit(err)
|
|
if len(details) > 0 {
|
|
outcome = outcome.WithExplanation(err.Error(), details...)
|
|
}
|
|
return outcome
|
|
}
|