package actions

import "errors"

// TerminalError marks an action failure as non-retryable: the action can never
// succeed no matter how many times it is re-invoked (e.g. the kernel reported a
// terminal task failure, or the business entity has been cancelled/deleted).
// The executor stops the whole executor-task through the abort machinery
// instead of rescheduling the action.
type TerminalError struct {
	Err error
}

func (e *TerminalError) Error() string {
	if e.Err == nil {
		return "terminal"
	}
	return "terminal: " + e.Err.Error()
}

func (e *TerminalError) Unwrap() error {
	return e.Err
}

// NewTerminalError wraps err so the executor treats it as non-retryable.
func NewTerminalError(err error) error {
	if err == nil {
		return nil
	}
	return &TerminalError{Err: err}
}

// IsTerminalError reports whether err (or any error it wraps) is a TerminalError.
func IsTerminalError(err error) bool {
	var terminalErr *TerminalError
	return errors.As(err, &terminalErr)
}
