Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmass committed Dec 19, 2024
1 parent 3758b73 commit c5f13f0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions opapi/src/generators/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ abstract class BaseApiError<Code extends ErrorCode, Type extends string, Descrip
public readonly type: Type,
public override readonly message: string,
public readonly error?: Error,
public readonly id?: string
public readonly id?: string,
public readonly metadata?: Record<string, unknown>,
) {
super(message)
Expand All @@ -80,6 +81,7 @@ abstract class BaseApiError<Code extends ErrorCode, Type extends string, Descrip
code: this.code,
type: this.type,
message: this.message,
metadata: this.metadata,
}
}
Expand All @@ -92,7 +94,7 @@ abstract class BaseApiError<Code extends ErrorCode, Type extends string, Descrip
.map(x => x.toString(16).padStart(2, '0'))
.join('')
.toUpperCase()
return \`\${prefix}_\${timestamp}x\${randomHexSuffix}\`
}
Expand Down Expand Up @@ -138,7 +140,7 @@ function getApiErrorFromObject(err: any) {
return new UnknownError(\`An unclassified API error occurred: \${err.message} (Type: \${err.type}, Code: \${err.code})\`)
}
return new ErrorClass(err.message, undefined, <string>err.id || 'UNKNOWN') // If error ID was not received do not pass undefined to generate a new one, flag it as UNKNOWN so we can fix the issue.
return new ErrorClass(err.message, undefined, <string>err.id || 'UNKNOWN', err.metadata) // If error ID was not received do not pass undefined to generate a new one, flag it as UNKNOWN so we can fix the issue.
}
return new UnknownError('An invalid error occurred: ' + JSON.stringify(err))
Expand All @@ -155,8 +157,8 @@ function generateError(error: ApiError) {
* ${description}
*/
export class ${error.type}Error extends BaseApiError<${error.status}, ${error.type}Type, '${description}'> {
constructor(message: string, error?: Error, id?: string) {
super(${error.status}, '${description}', '${error.type}', message, error, id)
constructor(message: string, error?: Error, id?: string, metadata?: Record<string, unknown>) {
super(${error.status}, '${description}', '${error.type}', message, error, id, metadata)
}
}\n`
}
Expand All @@ -170,7 +172,7 @@ function generateApiError(types: string[]) {
}

function generateErrorTypeMap(types: string[]) {
return `const errorTypes: { [type: string]: new (message: string, error?: Error, id?: string) => ApiError } = {
return `const errorTypes: { [type: string]: new (message: string, error?: Error, id?: string, metadata?: Record<string, unknown>) => ApiError } = {
${types.map((type) => ` ${type}: ${type}Error,`).join('\n')}
}\n`
}

0 comments on commit c5f13f0

Please sign in to comment.