Make PromptKit profile handling safer and more consistent
This commit is contained in:
@@ -45,16 +45,34 @@ type PromptKitProfileInspectionError struct {
|
||||
}
|
||||
|
||||
func (e *PromptKitProfileInspectionError) Error() string {
|
||||
if errors.Is(e.err, promptkit.ErrProfileNotFound) {
|
||||
switch {
|
||||
case errors.Is(e.err, promptkit.ErrProfileNotFound):
|
||||
return fmt.Sprintf("PromptKit profile %q is not configured", e.ProfileID)
|
||||
case errors.Is(e.err, promptkit.ErrInvalidRequest):
|
||||
return fmt.Sprintf("PromptKit profile ID %q is invalid", e.ProfileID)
|
||||
case errors.Is(e.err, promptkit.ErrProfileLoad):
|
||||
return fmt.Sprintf("PromptKit profile %q is invalid or unreadable", e.ProfileID)
|
||||
default:
|
||||
return fmt.Sprintf("PromptKit profile %q could not be inspected", e.ProfileID)
|
||||
}
|
||||
return fmt.Sprintf("inspect PromptKit profile %q: %v", e.ProfileID, e.err)
|
||||
}
|
||||
|
||||
func (e *PromptKitProfileInspectionError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
type promptKitProfileConfigurationError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *promptKitProfileConfigurationError) Error() string {
|
||||
return "PromptKit profile configuration is invalid or unreadable"
|
||||
}
|
||||
|
||||
func (e *promptKitProfileConfigurationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func NewPromptKitProfileInspector(cfg PromptKitProfileInspectorConfig) (*PromptKitProfileInspector, error) {
|
||||
source, options, err := promptKitProfileSourceEngineOptions(cfg.Source)
|
||||
if err != nil {
|
||||
@@ -74,7 +92,7 @@ func NewPromptKitProfileInspector(cfg PromptKitProfileInspectorConfig) (*PromptK
|
||||
ProfileDir: source.ProfileDir,
|
||||
}, options...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create PromptKit profile inspector: %w", err)
|
||||
return nil, &promptKitProfileConfigurationError{err: err}
|
||||
}
|
||||
return &PromptKitProfileInspector{engine: engine}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user