forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(datastore): add stub error maps (aws-amplify#9878)
- Loading branch information
Showing
5 changed files
with
70 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ErrorType } from '../../types'; | ||
|
||
export type ErrorMap = Partial<{ | ||
[key in ErrorType]: (error: Error) => boolean; | ||
}>; | ||
|
||
export const mutationErrorMap: ErrorMap = { | ||
BadRecord: error => /^Cannot return \w+ for [\w-_]+ type/.test(error.message), | ||
ConfigError: () => false, | ||
Transient: () => false, | ||
Unauthorized: () => false, | ||
}; | ||
|
||
export const subscriptionErrorMap: ErrorMap = { | ||
BadRecord: () => false, | ||
ConfigError: () => false, | ||
Transient: () => false, | ||
Unauthorized: (givenError: any) => { | ||
const { | ||
error: { errors: [{ message = '' } = {}] } = { | ||
errors: [], | ||
}, | ||
} = givenError; | ||
const regex = /Connection failed.+Unauthorized/; | ||
return regex.test(message); | ||
}, | ||
}; | ||
|
||
export const syncErrorMap: ErrorMap = { | ||
BadRecord: error => /^Cannot return \w+ for [\w-_]+ type/.test(error.message), | ||
ConfigError: () => false, | ||
Transient: () => false, | ||
Unauthorized: () => false, | ||
}; | ||
|
||
export function getMutationErrorType(error: Error): ErrorType { | ||
return mapErrorToType(mutationErrorMap, error); | ||
} | ||
|
||
export function getSubscriptionErrorType(error: Error): ErrorType { | ||
return mapErrorToType(subscriptionErrorMap, error); | ||
} | ||
|
||
export function getSyncErrorType(error: Error): ErrorType { | ||
return mapErrorToType(syncErrorMap, error); | ||
} | ||
|
||
/** | ||
* Categorizes an error with a broad error type, intended to make | ||
* customer error handling code simpler. | ||
* @param errorMap Error names and a list of patterns that indicate them (each pattern as a regex or function) | ||
* @param error The underying error to categorize. | ||
*/ | ||
export function mapErrorToType(errorMap: ErrorMap, error: Error): ErrorType { | ||
const errorTypes = [...Object.keys(errorMap)] as ErrorType[]; | ||
for (const errorType of errorTypes) { | ||
const matcher = errorMap[errorType]; | ||
if (matcher(error)) { | ||
return errorType; | ||
} | ||
} | ||
return 'Unknown'; | ||
} |
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