1- import {
2- AuthenticationError ,
3- NetworkError ,
4- ErrorCodes ,
5- isErrorWithCode ,
6- sanitizeErrorMessage ,
7- } from '@codebuff/sdk'
1+ import { sanitizeErrorMessage , getErrorStatusCode } from '@codebuff/sdk'
82
93/**
104 * Formats an unknown error into a user-facing markdown string.
115 *
12- * The goal is to provide clear, consistent messaging across the CLI while
13- * reusing the SDK error typing and sanitization logic.
6+ * The goal is to provide clear, consistent messaging across the CLI.
147 */
158export function formatErrorForDisplay ( error : unknown , fallbackTitle : string ) : string {
16- // Authentication-specific messaging
17- if ( error instanceof AuthenticationError ) {
18- if ( error . status === 401 ) {
19- return `${ fallbackTitle } : Authentication failed. Please check your API key.`
20- }
21-
22- if ( error . status === 403 ) {
23- return `${ fallbackTitle } : Access forbidden. You do not have permission to access this resource.`
24- }
9+ const statusCode = getErrorStatusCode ( error )
2510
26- return `${ fallbackTitle } : Invalid API key. Please check your credentials.`
11+ // Authentication-specific messaging based on statusCode
12+ if ( statusCode === 401 ) {
13+ return `${ fallbackTitle } : Authentication failed. Please check your API key.`
2714 }
28-
29- // Network-specific messaging
30- if ( error instanceof NetworkError ) {
31- let detail : string
32-
33- switch ( error . code ) {
34- case ErrorCodes . TIMEOUT :
35- detail = 'Request timed out. Please check your internet connection.'
36- break
37- case ErrorCodes . CONNECTION_REFUSED :
38- detail = 'Connection refused. The server may be down.'
39- break
40- case ErrorCodes . DNS_FAILURE :
41- detail = 'DNS resolution failed. Please check your internet connection.'
42- break
43- case ErrorCodes . SERVER_ERROR :
44- case ErrorCodes . SERVICE_UNAVAILABLE :
45- detail = 'Server error. Please try again later.'
46- break
47- case ErrorCodes . NETWORK_ERROR :
48- default :
49- detail = 'Network error. Please check your internet connection.'
50- break
51- }
52-
53- return `${ fallbackTitle } : ${ detail } `
15+ if ( statusCode === 403 ) {
16+ return `${ fallbackTitle } : Access forbidden. You do not have permission to access this resource.`
5417 }
5518
56- // Any other typed error that exposes a code
57- if ( isErrorWithCode ( error ) ) {
58- const safeMessage = sanitizeErrorMessage ( error )
59- return `${ fallbackTitle } : ${ safeMessage } `
19+ // Network/server error messaging based on statusCode
20+ if ( statusCode !== undefined ) {
21+ if ( statusCode === 408 ) {
22+ return `${ fallbackTitle } : Request timed out. Please check your internet connection.`
23+ }
24+ if ( statusCode === 503 ) {
25+ return `${ fallbackTitle } : Service unavailable. The server may be down.`
26+ }
27+ if ( statusCode >= 500 ) {
28+ return `${ fallbackTitle } : Server error. Please try again later.`
29+ }
30+ if ( statusCode === 429 ) {
31+ return `${ fallbackTitle } : Rate limited. Please try again later.`
32+ }
6033 }
6134
6235 // Generic Error instance
@@ -65,8 +38,9 @@ export function formatErrorForDisplay(error: unknown, fallbackTitle: string): st
6538 return `${ fallbackTitle } : ${ message } `
6639 }
6740
68- // Fallback for unknown values
69- return `${ fallbackTitle } : ${ String ( error ) } `
41+ // Try sanitizeErrorMessage for other cases
42+ const safeMessage = sanitizeErrorMessage ( error )
43+ return `${ fallbackTitle } : ${ safeMessage } `
7044}
7145
7246/**
0 commit comments