Skip to content

Commit

Permalink
wrap exceptions thrown in profile.map calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stackoverfloweth committed Mar 20, 2024
1 parent e9e3ca8 commit 86d2d3c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/createMapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Mapper, Profile, ProfileKey, RegisteredProfile, RegisteredProfiles, ProfileNotFoundError } from '@/types'
import { ProfileMappingError } from '@/types/profileMappingError'
import { asArray } from '@/utilities'

export function createMapper(): Mapper<RegisteredProfiles> {
Expand Down Expand Up @@ -41,7 +42,11 @@ export function createMapper(): Mapper<RegisteredProfiles> {
const map: Mapper<RegisteredProfiles>['map'] = (sourceKey, source, destinationKey) => {
const profile = getProfile(sourceKey, destinationKey)

return profile.map(source)
try {
return profile.map(source)
} catch (exception) {
throw new ProfileMappingError(profile, { cause: exception })
}
}

const mapMany: Mapper<RegisteredProfiles>['mapMany'] = (sourceKey, sourceArray, destinationKey) => {
Expand Down
17 changes: 17 additions & 0 deletions src/createmapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from 'vitest'
import mapper from '@/index'
import { Profile } from '@/types'
import { ProfileMappingError } from '@/types/profileMappingError'

const stringToBoolean = {
sourceKey: 'string',
Expand Down Expand Up @@ -32,4 +33,20 @@ test('register works with single profile', () => {
mapper.register(singleProfile)

expect(mapper.has('foo', 'bar')).toBe(true)
})

test('when map function throws exception, wraps in ProfileMappingError', () => {
const singleProfile = {
sourceKey: 'foo',
destinationKey: 'bar',
map: () => {
throw 'not implemented'
},
} as const satisfies Profile

mapper.register(singleProfile)

const action: () => void = () => mapper.map('foo', null, 'bar')

expect(action).toThrow(ProfileMappingError)
})
7 changes: 7 additions & 0 deletions src/types/profileMappingError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Profile } from '@/types/profile'

export class ProfileMappingError extends Error {
public constructor(profile: Profile, options?: ErrorOptions) {
super(`Failed to Execute map for source "${profile.sourceKey}" and destination "${profile.destinationKey}"`, options)
}
}

0 comments on commit 86d2d3c

Please sign in to comment.