From f57796e06df96f29730cd2f757f4b30baa91cb3b Mon Sep 17 00:00:00 2001 From: Matt Gramigna Date: Sat, 23 Dec 2023 13:44:07 -0500 Subject: [PATCH] chore(services): use more specific error format --- packages/api/src/router/patient.ts | 3 +- packages/canvas/src/services/allergen.ts | 4 +- packages/canvas/src/services/careplan.ts | 4 +- packages/canvas/src/services/careteam.ts | 2 +- packages/canvas/src/services/claim.ts | 6 +- packages/canvas/src/services/communication.ts | 2 +- packages/canvas/src/services/consent.ts | 2 +- .../services/coverageeligibilityrequest.ts | 6 +- .../services/coverageeligibilityresponse.ts | 6 +- packages/canvas/src/services/device.ts | 4 +- .../canvas/src/services/diagnosticreport.ts | 4 +- .../canvas/src/services/documentreference.ts | 4 +- packages/canvas/src/services/encounter.ts | 4 +- packages/canvas/src/services/goal.ts | 4 +- packages/canvas/src/services/immunization.ts | 4 +- packages/canvas/src/services/location.ts | 4 +- packages/canvas/src/services/media.ts | 2 +- packages/canvas/src/services/medication.ts | 4 +- .../canvas/src/services/medicationrequest.ts | 4 +- packages/canvas/src/services/observation.ts | 2 +- packages/canvas/src/services/organization.ts | 4 +- packages/canvas/src/services/paymentnotice.ts | 2 +- packages/canvas/src/services/practitioner.ts | 4 +- packages/canvas/src/services/procedure.ts | 4 +- packages/canvas/src/services/provenance.ts | 4 +- packages/canvas/src/services/questionnaire.ts | 4 +- packages/canvas/src/services/schedule.ts | 6 +- packages/canvas/src/services/slot.ts | 6 +- packages/canvas/src/services/task.ts | 6 +- packages/canvas/src/types/errors.ts | 6 ++ packages/canvas/src/types/service.ts | 10 ++-- packages/canvas/src/utils/fetch.ts | 58 ++++++++++++++----- 32 files changed, 113 insertions(+), 76 deletions(-) create mode 100644 packages/canvas/src/types/errors.ts diff --git a/packages/api/src/router/patient.ts b/packages/api/src/router/patient.ts index 1abbe42..84cb102 100644 --- a/packages/api/src/router/patient.ts +++ b/packages/api/src/router/patient.ts @@ -28,8 +28,7 @@ export const createPatientRouter = ({ patientService }: { patientService: Patien }); if (result.isErr()) { - // TODO - throw new Error(result.error); + throw new Error(`[${result.error.errorType}] - ${result.error.details}`); } return result.value; diff --git a/packages/canvas/src/services/allergen.ts b/packages/canvas/src/services/allergen.ts index a4b483b..f2aab2e 100644 --- a/packages/canvas/src/services/allergen.ts +++ b/packages/canvas/src/services/allergen.ts @@ -30,11 +30,11 @@ export const AllergenService = ({ baseUrl }: { baseUrl: string }): AllergenServi }; const create: AllergenServiceType['create'] = async () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: AllergenServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: AllergenServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/careplan.ts b/packages/canvas/src/services/careplan.ts index 3cfa0b0..96a50cc 100644 --- a/packages/canvas/src/services/careplan.ts +++ b/packages/canvas/src/services/careplan.ts @@ -30,11 +30,11 @@ export const CarePlanService = ({ baseUrl }: { baseUrl: string }): CarePlanServi }; const create: CarePlanServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: CarePlanServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: CarePlanServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/careteam.ts b/packages/canvas/src/services/careteam.ts index ed44d25..336d0f7 100644 --- a/packages/canvas/src/services/careteam.ts +++ b/packages/canvas/src/services/careteam.ts @@ -30,7 +30,7 @@ export const CareTeamService = ({ baseUrl }: { baseUrl: string }): CareTeamServi }; const create: CareTeamServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: CareTeamServiceType['update'] = async ({ resource, accessToken }) => { diff --git a/packages/canvas/src/services/claim.ts b/packages/canvas/src/services/claim.ts index 6825cc6..89cb0b1 100644 --- a/packages/canvas/src/services/claim.ts +++ b/packages/canvas/src/services/claim.ts @@ -8,7 +8,7 @@ export type ClaimServiceType = Service; export const ClaimService = ({ baseUrl }: { baseUrl: string }): ClaimServiceType => { const read: ClaimServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const create: ClaimServiceType['create'] = async ({ resource, accessToken }) => { @@ -22,11 +22,11 @@ export const ClaimService = ({ baseUrl }: { baseUrl: string }): ClaimServiceType }; const update: ClaimServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ClaimServiceType['search'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; return { diff --git a/packages/canvas/src/services/communication.ts b/packages/canvas/src/services/communication.ts index d6906a0..07234d7 100644 --- a/packages/canvas/src/services/communication.ts +++ b/packages/canvas/src/services/communication.ts @@ -44,7 +44,7 @@ export const CommunicationService = ({ }; const update: CommunicationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: CommunicationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/consent.ts b/packages/canvas/src/services/consent.ts index 6be4369..30491f6 100644 --- a/packages/canvas/src/services/consent.ts +++ b/packages/canvas/src/services/consent.ts @@ -34,7 +34,7 @@ export const ConsentService = ({ baseUrl }: { baseUrl: string }): ConsentService }; const update: ConsentServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ConsentServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/coverageeligibilityrequest.ts b/packages/canvas/src/services/coverageeligibilityrequest.ts index 81e9cb1..6d78375 100644 --- a/packages/canvas/src/services/coverageeligibilityrequest.ts +++ b/packages/canvas/src/services/coverageeligibilityrequest.ts @@ -18,7 +18,7 @@ export const CoverageEligibilityRequestService = ({ baseUrl: string; }): CoverageEligibilityRequestServiceType => { const read: CoverageEligibilityRequestServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const create: CoverageEligibilityRequestServiceType['create'] = async ({ @@ -35,11 +35,11 @@ export const CoverageEligibilityRequestService = ({ }; const update: CoverageEligibilityRequestServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: CoverageEligibilityRequestServiceType['search'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; return { diff --git a/packages/canvas/src/services/coverageeligibilityresponse.ts b/packages/canvas/src/services/coverageeligibilityresponse.ts index 6a27583..31ff083 100644 --- a/packages/canvas/src/services/coverageeligibilityresponse.ts +++ b/packages/canvas/src/services/coverageeligibilityresponse.ts @@ -24,15 +24,15 @@ export const CoverageEligibilityResponseService = ({ baseUrl: string; }): CoverageEligibilityResponseServiceType => { const read: CoverageEligibilityResponseServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const create: CoverageEligibilityResponseServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: CoverageEligibilityResponseServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: CoverageEligibilityResponseServiceType['search'] = async ({ diff --git a/packages/canvas/src/services/device.ts b/packages/canvas/src/services/device.ts index 8bf31d4..9711fa7 100644 --- a/packages/canvas/src/services/device.ts +++ b/packages/canvas/src/services/device.ts @@ -24,11 +24,11 @@ export const DeviceService = ({ baseUrl }: { baseUrl: string }): DeviceServiceTy }; const create: DeviceServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: DeviceServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: DeviceServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/diagnosticreport.ts b/packages/canvas/src/services/diagnosticreport.ts index fcdb61e..5e3ed01 100644 --- a/packages/canvas/src/services/diagnosticreport.ts +++ b/packages/canvas/src/services/diagnosticreport.ts @@ -34,11 +34,11 @@ export const DiagnosticReportService = ({ }; const create: DiagnosticReportServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: DiagnosticReportServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: DiagnosticReportServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/documentreference.ts b/packages/canvas/src/services/documentreference.ts index 4f9d47a..53b6ef2 100644 --- a/packages/canvas/src/services/documentreference.ts +++ b/packages/canvas/src/services/documentreference.ts @@ -34,11 +34,11 @@ export const DocumentReferenceService = ({ }; const create: DocumentReferenceServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: DocumentReferenceServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: DocumentReferenceServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/encounter.ts b/packages/canvas/src/services/encounter.ts index 358a756..1254540 100644 --- a/packages/canvas/src/services/encounter.ts +++ b/packages/canvas/src/services/encounter.ts @@ -30,11 +30,11 @@ export const EncounterService = ({ baseUrl }: { baseUrl: string }): EncounterSer }; const create: EncounterServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: EncounterServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: EncounterServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/goal.ts b/packages/canvas/src/services/goal.ts index a77a877..5422f32 100644 --- a/packages/canvas/src/services/goal.ts +++ b/packages/canvas/src/services/goal.ts @@ -24,11 +24,11 @@ export const GoalService = ({ baseUrl }: { baseUrl: string }): GoalServiceType = }; const create: GoalServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: GoalServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: GoalServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/immunization.ts b/packages/canvas/src/services/immunization.ts index eedef14..9dc575b 100644 --- a/packages/canvas/src/services/immunization.ts +++ b/packages/canvas/src/services/immunization.ts @@ -30,11 +30,11 @@ export const ImmunizationService = ({ baseUrl }: { baseUrl: string }): Immunizat }; const create: ImmunizationServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: ImmunizationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ImmunizationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/location.ts b/packages/canvas/src/services/location.ts index 640af47..aac1123 100644 --- a/packages/canvas/src/services/location.ts +++ b/packages/canvas/src/services/location.ts @@ -30,11 +30,11 @@ export const LocationService = ({ baseUrl }: { baseUrl: string }): LocationServi }; const create: LocationServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: LocationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: LocationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/media.ts b/packages/canvas/src/services/media.ts index 30532ad..840f3f1 100644 --- a/packages/canvas/src/services/media.ts +++ b/packages/canvas/src/services/media.ts @@ -34,7 +34,7 @@ export const MediaService = ({ baseUrl }: { baseUrl: string }): MediaServiceType }; const update: MediaServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: MediaServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/medication.ts b/packages/canvas/src/services/medication.ts index cdd3abb..64468d8 100644 --- a/packages/canvas/src/services/medication.ts +++ b/packages/canvas/src/services/medication.ts @@ -30,11 +30,11 @@ export const MedicationService = ({ baseUrl }: { baseUrl: string }): MedicationS }; const create: MedicationServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: MedicationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: MedicationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/medicationrequest.ts b/packages/canvas/src/services/medicationrequest.ts index 49abe36..b11fb9a 100644 --- a/packages/canvas/src/services/medicationrequest.ts +++ b/packages/canvas/src/services/medicationrequest.ts @@ -34,11 +34,11 @@ export const MedicationRequestService = ({ }; const create: MedicationRequestServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: MedicationRequestServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: MedicationRequestServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/observation.ts b/packages/canvas/src/services/observation.ts index d9610c1..20a7662 100644 --- a/packages/canvas/src/services/observation.ts +++ b/packages/canvas/src/services/observation.ts @@ -40,7 +40,7 @@ export const ObservationService = ({ baseUrl }: { baseUrl: string }): Observatio }; const update: ObservationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ObservationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/organization.ts b/packages/canvas/src/services/organization.ts index 226151e..83e75a6 100644 --- a/packages/canvas/src/services/organization.ts +++ b/packages/canvas/src/services/organization.ts @@ -30,11 +30,11 @@ export const OrganizationService = ({ baseUrl }: { baseUrl: string }): Organizat }; const create: OrganizationServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: OrganizationServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: OrganizationServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/paymentnotice.ts b/packages/canvas/src/services/paymentnotice.ts index 6f76e51..4f0ab60 100644 --- a/packages/canvas/src/services/paymentnotice.ts +++ b/packages/canvas/src/services/paymentnotice.ts @@ -44,7 +44,7 @@ export const PaymentNoticeService = ({ }; const update: PaymentNoticeServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: PaymentNoticeServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/practitioner.ts b/packages/canvas/src/services/practitioner.ts index b61e954..23acc17 100644 --- a/packages/canvas/src/services/practitioner.ts +++ b/packages/canvas/src/services/practitioner.ts @@ -30,11 +30,11 @@ export const PractitionerService = ({ baseUrl }: { baseUrl: string }): Practitio }; const create: PractitionerServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: PractitionerServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: PractitionerServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/procedure.ts b/packages/canvas/src/services/procedure.ts index 1014547..f76cc75 100644 --- a/packages/canvas/src/services/procedure.ts +++ b/packages/canvas/src/services/procedure.ts @@ -30,11 +30,11 @@ export const ProcedureService = ({ baseUrl }: { baseUrl: string }): ProcedureSer }; const create: ProcedureServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: ProcedureServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ProcedureServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/provenance.ts b/packages/canvas/src/services/provenance.ts index c4a8b6c..b24aacd 100644 --- a/packages/canvas/src/services/provenance.ts +++ b/packages/canvas/src/services/provenance.ts @@ -30,11 +30,11 @@ export const ProvenanceService = ({ baseUrl }: { baseUrl: string }): ProvenanceS }; const create: ProvenanceServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: ProvenanceServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ProvenanceServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/questionnaire.ts b/packages/canvas/src/services/questionnaire.ts index 174acd7..c8a2ffb 100644 --- a/packages/canvas/src/services/questionnaire.ts +++ b/packages/canvas/src/services/questionnaire.ts @@ -34,11 +34,11 @@ export const QuestionnaireService = ({ }; const create: QuestionnaireServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: QuestionnaireServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: QuestionnaireServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/schedule.ts b/packages/canvas/src/services/schedule.ts index 7587844..31d1c65 100644 --- a/packages/canvas/src/services/schedule.ts +++ b/packages/canvas/src/services/schedule.ts @@ -20,15 +20,15 @@ export type ScheduleServiceType = Service< export const ScheduleService = ({ baseUrl }: { baseUrl: string }): ScheduleServiceType => { const read: ScheduleServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const create: ScheduleServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: ScheduleServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: ScheduleServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/slot.ts b/packages/canvas/src/services/slot.ts index babb4d8..b663cb0 100644 --- a/packages/canvas/src/services/slot.ts +++ b/packages/canvas/src/services/slot.ts @@ -14,15 +14,15 @@ export type SlotServiceType = Service { const read: SlotServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const create: SlotServiceType['create'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const update: SlotServiceType['update'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve(err({ errorType: 'UNSUPPORTED' })); }; const search: SlotServiceType['search'] = async ({ accessToken, args }) => { diff --git a/packages/canvas/src/services/task.ts b/packages/canvas/src/services/task.ts index 5daf5cd..d9efdcf 100644 --- a/packages/canvas/src/services/task.ts +++ b/packages/canvas/src/services/task.ts @@ -14,7 +14,11 @@ export type TaskServiceType = Service { const read: TaskServiceType['read'] = () => { - return Promise.resolve(err('NOT_SUPPORTED')); + return Promise.resolve( + err({ + errorType: 'UNSUPPORTED', + }), + ); }; const create: TaskServiceType['create'] = async ({ resource, accessToken }) => { diff --git a/packages/canvas/src/types/errors.ts b/packages/canvas/src/types/errors.ts new file mode 100644 index 0000000..d78db41 --- /dev/null +++ b/packages/canvas/src/types/errors.ts @@ -0,0 +1,6 @@ +export type CanvasErrorType = 'PARSE' | 'BAD_REQUEST' | 'UNKNOWN' | 'UNSUPPORTED' | 'INTERNAL'; + +export interface CanvasError { + errorType: CanvasErrorType; + details?: string; +} diff --git a/packages/canvas/src/types/service.ts b/packages/canvas/src/types/service.ts index ead9566..4372ef5 100644 --- a/packages/canvas/src/types/service.ts +++ b/packages/canvas/src/types/service.ts @@ -1,5 +1,7 @@ import { type Result } from 'neverthrow'; +import { type CanvasError } from './errors'; + interface BaseArgs { accessToken: string; } @@ -21,8 +23,8 @@ interface SearchArgs extends BaseArgs { } export interface Service { - read: (args: ReadArgs) => Promise>; - create: (args: CreateArgs) => Promise>; - update: (args: UpdateArgs) => Promise>; - search: (args: SearchArgs) => Promise>; + read: (args: ReadArgs) => Promise>; + create: (args: CreateArgs) => Promise>; + update: (args: UpdateArgs) => Promise>; + search: (args: SearchArgs) => Promise>; } diff --git a/packages/canvas/src/utils/fetch.ts b/packages/canvas/src/utils/fetch.ts index 2b99422..668d3f4 100644 --- a/packages/canvas/src/utils/fetch.ts +++ b/packages/canvas/src/utils/fetch.ts @@ -1,6 +1,8 @@ import { err, ok, type Result } from 'neverthrow'; import { ZodError, type ZodType, type ZodTypeDef } from 'zod'; +import { type CanvasError } from '../types/errors'; + export async function makeFhirGetRequest( schema: ZodType, args: { @@ -8,7 +10,7 @@ export async function makeFhirGetRequest( token: string; query?: string; }, -): Promise> { +): Promise> { const queryString = args.query ? `?${args.query}` : ''; try { @@ -28,16 +30,23 @@ export async function makeFhirGetRequest( return ok(schema.parse(await response.json())); } catch (e) { - // TODO: fill these in if (e instanceof ZodError) { - return err('parse'); + return err({ + errorType: 'PARSE', + details: e.message, + }); } if (e instanceof Error) { - return err(e.message); + return err({ + errorType: 'UNKNOWN', + details: e.message, + }); } - return err('bad'); + return err({ + errorType: 'UNKNOWN', + }); } } @@ -45,7 +54,7 @@ export async function makeFhirCreateRequest(args: { path: string; token: string; body?: T; -}): Promise> { +}): Promise> { try { console.log(`[canvas] POST ${args.path}`); const response = await fetch(args.path, { @@ -66,7 +75,10 @@ export async function makeFhirCreateRequest(args: { const location = response.headers.get('location'); if (!location) { - return err('todo'); + return err({ + errorType: 'INTERNAL', + details: 'Could not parse location from response headers', + }); } const locationWithoutHistory = location.replace(/\/_history.*/, ''); @@ -74,16 +86,23 @@ export async function makeFhirCreateRequest(args: { return ok(id); } catch (e) { - // TODO: fill these in if (e instanceof ZodError) { - return err('bad'); + return err({ + errorType: 'PARSE', + details: e.message, + }); } if (e instanceof Error) { - return err(e.message); + return err({ + errorType: 'UNKNOWN', + details: e.message, + }); } - return err('bad'); + return err({ + errorType: 'UNKNOWN', + }); } } @@ -91,7 +110,7 @@ export async function makeFhirUpdateRequest(args: { path: string; token: string; body?: T; -}): Promise> { +}): Promise> { try { console.log(`[canvas] PUT ${args.path}`); const response = await fetch(args.path, { @@ -111,15 +130,22 @@ export async function makeFhirUpdateRequest(args: { return ok(null); } catch (e) { - // TODO: fill these in if (e instanceof ZodError) { - return err('bad'); + return err({ + errorType: 'PARSE', + details: e.message, + }); } if (e instanceof Error) { - return err(e.message); + return err({ + errorType: 'UNKNOWN', + details: e.message, + }); } - return err('bad'); + return err({ + errorType: 'UNKNOWN', + }); } }