Files
opencode-manager/backend/src/utils/error-utils.ts
T
Chris Scott 8166044d6e Fix eslint (#46)
* Fix mobile image upload and paste with improved file type handling

* Replace older sessions header with separator line

* Add ESLint no-console rules and fix lint errors

- Add eslint configuration for backend with no-console rule
- Add reusable error type guard utility
- Replace all error: any with error: unknown and type guards across backend
- Update logger to use console.warn/console.error only
- Remove unused variables and dead code functions
- Fix regex escape characters
- Update AGENTS.md with lint commands and requirements
2026-01-03 15:19:28 -05:00

34 lines
960 B
TypeScript

export function isError(error: unknown): error is Error {
return error instanceof Error
}
export function getErrorMessage(error: unknown): string {
if (isError(error)) {
return error.message
}
return String(error)
}
export function getErrorStack(error: unknown): string | undefined {
if (isError(error)) {
return error.stack
}
return undefined
}
interface ErrorWithStatusCode {
statusCode?: number
status?: number
}
const VALID_STATUS_CODES = new Set([200, 201, 204, 301, 302, 304, 400, 401, 403, 404, 405, 409, 413, 422, 500, 501, 502, 503])
export function getStatusCode(error: unknown): number {
let code = 500
if (error && typeof error === 'object' && 'statusCode' in error) {
code = (error as ErrorWithStatusCode).statusCode || 500
} else if (error && typeof error === 'object' && 'status' in error) {
code = (error as ErrorWithStatusCode).status || 500
}
return VALID_STATUS_CODES.has(code) ? code : 500
}