-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
173 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { BaseError, type BaseErrorType } from './base'; | ||
export { | ||
UserRejectionError, | ||
type UserRejectionErrorType, | ||
} from './userRejection'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { UserRejectionError } from './userRejection'; | ||
|
||
test('userRejection', () => { | ||
const operationName = 'operation'; | ||
expect( | ||
new UserRejectionError({ | ||
operationName: operationName, | ||
}).message, | ||
).toBe(`The operation ${operationName} was rejected by the user.`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BaseError } from './base'; | ||
|
||
export type UserRejectionErrorType = UserRejectionError & { | ||
name: 'UserRejectionError'; | ||
}; | ||
|
||
export class UserRejectionError extends BaseError { | ||
override name = 'UserRejectionError'; | ||
|
||
constructor({ operationName }) { | ||
super(`The operation ${operationName} was rejected by the user.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { UserRejectionError } from '../userRejection'; | ||
import { isUserRejectionError, operationErrorMapping } from './errorMapping'; | ||
|
||
const massaUserRejectionErrorMessage = 'Action canceled by user'; | ||
const bearbyUserRejectionErrorMessage = 'User rejected'; | ||
const someOtherErrorMessage = 'Some other error'; | ||
|
||
describe('isUserRejectionError', () => { | ||
it('should return true if error message includes "User rejected"', () => { | ||
const error = new Error(massaUserRejectionErrorMessage); | ||
expect(isUserRejectionError(error)).toBe(true); | ||
}); | ||
|
||
it('should return true if error message includes "Action canceled by user"', () => { | ||
const error = new Error(bearbyUserRejectionErrorMessage); | ||
expect(isUserRejectionError(error)).toBe(true); | ||
}); | ||
|
||
it('should return false if error message does not include specific rejection messages', () => { | ||
const error = new Error(someOtherErrorMessage); | ||
expect(isUserRejectionError(error)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('operationErrorMapping', () => { | ||
it('should return a UserRejectionError if the error is a user rejection error', () => { | ||
const error = new Error('User rejected'); | ||
const mappedError = operationErrorMapping('someOperation', error); | ||
expect(mappedError).toBeInstanceOf(UserRejectionError); | ||
expect(mappedError.message).toBe( | ||
'The operation someOperation was rejected by the user.', | ||
); | ||
}); | ||
|
||
it('should return the original error if the error is not a user rejection error', () => { | ||
const error = new Error(someOtherErrorMessage); | ||
const mappedError = operationErrorMapping('someOperation', error); | ||
expect(mappedError).toBe(error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { UserRejectionError } from '../userRejection'; | ||
|
||
export function isUserRejectionError(error: Error): boolean { | ||
const bearbyUserRejectionErrorMessage = 'User rejected'; | ||
const massaUserRejectionErrorMessage = 'Action canceled by user'; | ||
return ( | ||
error.message && | ||
(error.message.includes(bearbyUserRejectionErrorMessage) || | ||
error.message.includes(massaUserRejectionErrorMessage)) | ||
); | ||
} | ||
|
||
export function operationErrorMapping( | ||
operationName: string, | ||
error: Error, | ||
): Error { | ||
if (isUserRejectionError(error)) { | ||
return new UserRejectionError({ | ||
operationName: operationName, | ||
}); | ||
} else { | ||
return error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters