37 lines
926 B
Go
37 lines
926 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.franklin.lab/steve.cliff/pcli/model"
|
|
)
|
|
|
|
// friendlyAPIError translates APIError status codes into human-readable messages with operation context.
|
|
func friendlyAPIError(err error, operation string, permissionHint string) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
// Check if it's an APIError
|
|
apiErr, ok := err.(*model.APIError)
|
|
if !ok {
|
|
// Not an API error, return as-is
|
|
return err
|
|
}
|
|
|
|
switch apiErr.StatusCode {
|
|
case 401:
|
|
return fmt.Errorf("%s: authentication failed — check your API key", operation)
|
|
case 403:
|
|
if permissionHint != "" {
|
|
return fmt.Errorf("%s: permission denied (%s)", operation, permissionHint)
|
|
}
|
|
return fmt.Errorf("%s: permission denied", operation)
|
|
case 404:
|
|
return fmt.Errorf("%s: not found — the resource may not exist or you may not have access to it", operation)
|
|
default:
|
|
// Unknown status code, return original error
|
|
return err
|
|
}
|
|
}
|