package scriptorium import ( "errors" "fmt" "gitea.maximumdirect.net/eric/scriptorium/internal/profile" "gitea.maximumdirect.net/eric/scriptorium/internal/promptdef" "gitea.maximumdirect.net/eric/scriptorium/internal/usecase" ) func mapPublicError(err error) error { if err == nil { return nil } if hasPublicError(err) { return err } publicErr := publicErrorFor(err) if publicErr == nil { return err } return fmt.Errorf("%w: %w", publicErr, err) } func hasPublicError(err error) bool { for _, publicErr := range []error{ ErrInvalidConfig, ErrInvalidRequest, ErrPromptNotFound, ErrProfileNotFound, ErrPromptLoad, ErrProfileLoad, ErrArtifactLoad, ErrPromptRender, ErrLLMGenerate, ErrValidation, } { if errors.Is(err, publicErr) { return true } } return false } func publicErrorFor(err error) error { switch { case errors.Is(err, promptdef.ErrPromptDefinitionNotFound): return ErrPromptNotFound case errors.Is(err, profile.ErrProfileNotFound): return ErrProfileNotFound case errors.Is(err, promptdef.ErrInvalidYAML), errors.Is(err, promptdef.ErrInvalidPromptDefinition): return ErrPromptLoad case isProfileLoadCause(err): return ErrProfileLoad case errors.Is(err, usecase.ErrArtifactLoad): return ErrArtifactLoad case errors.Is(err, usecase.ErrPromptRender): return ErrPromptRender case errors.Is(err, usecase.ErrLLMGenerate): return ErrLLMGenerate case errors.Is(err, usecase.ErrValidation): return ErrValidation case errors.Is(err, usecase.ErrInvalidRequest): return ErrInvalidRequest case errors.Is(err, usecase.ErrProfileLoad): return ErrPromptLoad default: return nil } } func isProfileLoadCause(err error) bool { return errors.Is(err, profile.ErrInvalidYAML) || errors.Is(err, profile.ErrInvalidProfile) || errors.Is(err, profile.ErrRawAPIKeyNotAllowed) }