@oystehr/sdk
Version:
Oystehr SDK
58 lines (54 loc) • 1.7 kB
text/typescript
import { OperationOutcome as OperationOutcomeR4B } from 'fhir/r4b';
import { OperationOutcome as OperationOutcomeR5 } from 'fhir/r5';
export class OystehrSdkError extends Error {
code: number;
constructor({ message, code, cause }: { message: string; code: number; cause?: unknown }) {
super(message, { cause });
Object.setPrototypeOf(this, OystehrSdkError.prototype);
this.code = code;
this.name = 'OystehrSdkError';
}
toString(): string {
return `${this.name}: ${this.message} (code: ${this.code})`;
}
toJSON(): any {
return {
name: this.name,
message: this.message,
code: this.code,
cause: this.cause,
};
}
}
function transformOperationOutcomeToErrorMessage(cause: OperationOutcomeR4B | OperationOutcomeR5): string {
const causes = [];
for (const issue of cause.issue ?? []) {
if (issue.details && issue.details.text) {
causes.push(issue.details.text);
}
}
if (!causes.length) {
causes.push('Unknown FHIR error');
}
return causes.join(',');
}
export class OystehrFHIRError extends OystehrSdkError {
cause: OperationOutcomeR4B | OperationOutcomeR5;
constructor({ error, code }: { error: OperationOutcomeR4B | OperationOutcomeR5; code: number }) {
super({
message: transformOperationOutcomeToErrorMessage(error as OperationOutcomeR4B | OperationOutcomeR5),
code,
});
Object.setPrototypeOf(this, OystehrFHIRError.prototype);
this.cause = error as OperationOutcomeR4B | OperationOutcomeR5;
this.name = 'OystehrFHIRError';
}
toJSON(): any {
return {
name: this.name,
message: this.message,
code: this.code,
cause: this.cause,
};
}
}