0
Fork 0
mirror of https://github.com/penpot/penpot-export.git synced 2025-01-22 22:48:42 -05:00

feat(cli): show a meaninfull error for Penpot API error responses

This commit is contained in:
Roberto Redradix 2023-08-22 17:11:54 +02:00
parent 1754228058
commit 218e01636f
2 changed files with 31 additions and 0 deletions

View file

@ -11,8 +11,23 @@ import type {
PenpotSettings,
FetcherOptions,
PenpotGetPageOptions,
PenpotApiErrorResponse,
} from './types'
class PenpotApiError extends Error {
constructor(parsedError: PenpotApiErrorResponse) {
super(
`Penpot API call failed: error type "${parsedError.type}" with code "${parsedError.code}".`,
)
}
}
class PenpotApiUnknownError extends Error {
constructor() {
super('Unknown Penpot API error.')
}
}
export class Penpot {
private accessToken: string
@ -38,6 +53,17 @@ export class Penpot {
`https://design.penpot.app/api/rpc/command/${command}`,
config,
)
if (!response.ok) {
const error = await response
.clone()
.json()
.then((parsedError) => new PenpotApiError(parsedError))
.catch(() => new PenpotApiUnknownError())
throw error
}
const json = await response.json()
return json as ResultType

View file

@ -26,4 +26,9 @@ export interface PenpotPage {
objects: Record<string, PenpotObject>
}
export interface PenpotApiErrorResponse {
type: string
code: string
}
export type PenpotComponent = PenpotObject & { objects: PenpotObject[] }