diff --git a/CLIENT-ERRORS-SAMPLE.md b/CLIENT-ERRORS-SAMPLE.md index 898b0c0..a392693 100644 --- a/CLIENT-ERRORS-SAMPLE.md +++ b/CLIENT-ERRORS-SAMPLE.md @@ -59,7 +59,7 @@ const findCodes = (error: ApolloError | GraphQLError): PropheticErrorCode[] => { return [PropheticErrorCode.CodeLessError]; } -export const errorHere = (error: ApolloError | GraphQLError | undefined ) => { +export const thisError = (error: ApolloError | GraphQLError | undefined ) => { if(!error) { return new PropheticError([]); } diff --git a/README.md b/README.md index 84b8d14..f40c7d8 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ * Generate **Server-side** **throwable** errors in your resolvers like `throw new NotAProphetError()` * Expose **machine readable** graphql errors through your api documentation -* Generate **Client-side** Apollo errors **consumable** like `errorHere(error).isNotAProphetError ?` +* Generate **Client-side** Apollo errors **consumable** like `thisError(error).isNotAProphetError ?` # 📋 Table of Contents @@ -188,20 +188,20 @@ apollo-prophecy ask http://localhost:3000/graphql #### Helpers -In order to easily handle erros with **Apollo-Client**, the generated `Errors.ts` exposes two helpers methods `errorHere` and `isThis`, both methods takes one paramater of type `ApolloError` or `GraphQLError`. +In order to easily handle erros with **Apollo-Client**, the generated `Errors.ts` exposes two helpers methods `thisError` and `isThis`, both methods takes one paramater of type `ApolloError` or `GraphQLError`. -##### `errorHere()` function +##### `thisError()` function -`errorHere` returns an object that has a **property** named after each errors. +`thisError` returns an object that has a **property** named after each errors. You can perform a simple `boolean` check on the `error` argument by calling the approiate *key*. ```ts -import { errorHere } from `./_generated/Errors.ts`; +import { thisError } from `./_generated/Errors.ts`; ...(error) => { - if(errorHere(error).isUserNotFoundError){ + if(thisError(error).isUserNotFoundError){ // Do something - } else if(errorHere(error).isNotAProphetError){ + } else if(thisError(error).isNotAProphetError){ // Do something else } } diff --git a/lib/commands/ask/createRawFileFromEntries.js b/lib/commands/ask/createRawFileFromEntries.js index 3f4dbfd..c3f6f0b 100644 --- a/lib/commands/ask/createRawFileFromEntries.js +++ b/lib/commands/ask/createRawFileFromEntries.js @@ -7,7 +7,7 @@ var PropheticErrorDef = "\nexport class PropheticError {\n constructor(public c var PropheticErrorGetterDef = "get is$_ENUM_$() { return this.inCodes(PropheticErrorCode.$_ENUM_$); }"; var PropheticErrorHandledDef = "\nexport interface Handler {\n (): any\n}\n\nexport class PropheticErrorHandled {\n private handler: Handler = () => {}\n\n constructor(public codes: string[]){}\n\n private inCodes(code: PropheticErrorCode, handler: Handler){\n if(this.codes.indexOf(code) > -1){\n this.handler = handler\n }\n\n return this;\n }\n\n CodeLessError(handler: Handler) { return this.inCodes(PropheticErrorCode.CodeLessError, handler); }\n _FUNCTIONS_\n handle() { return this.handler(); }\n}"; var PropheticErrorHandlerDef = "$_ENUM_$(handler: Handler) { return this.inCodes(PropheticErrorCode.$_ENUM_$, handler); }"; -var BottomExportedMethods = "\nconst CODE_LESS_EXTENSION = { code: 'NONE'};\nconst findCodes = (error: ApolloError | GraphQLError): PropheticErrorCode[] => {\n if(error instanceof ApolloError) {\n return error.graphQLErrors.map((gError) => findCodes(gError)[0]);\n } else if(error.extensions) {\n const { extensions: { code } = CODE_LESS_EXTENSION } = error;\n return [code];\n }\n\n return [PropheticErrorCode.CodeLessError];\n}\n\nexport const errorHere = (error: ApolloError | GraphQLError | undefined ) => {\n if(!error) {\n return new PropheticError([]);\n }\n const codes = findCodes(error);\n return new PropheticError(codes);\n}\n\nexport const isThis = (error: ApolloError | GraphQLError | undefined) => {\n if(!error) {\n return new PropheticErrorHandled([]);\n }\n const codes = findCodes(error);\n return new PropheticErrorHandled(codes);\n}"; +var BottomExportedMethods = "\nconst CODE_LESS_EXTENSION = { code: 'NONE'};\nconst findCodes = (error: ApolloError | GraphQLError): PropheticErrorCode[] => {\n if(error instanceof ApolloError) {\n return error.graphQLErrors.map((gError) => findCodes(gError)[0]);\n } else if(error.extensions) {\n const { extensions: { code } = CODE_LESS_EXTENSION } = error;\n return [code];\n }\n\n return [PropheticErrorCode.CodeLessError];\n}\n\nexport const thisError = (error: ApolloError | GraphQLError | undefined ) => {\n if(!error) {\n return new PropheticError([]);\n }\n const codes = findCodes(error);\n return new PropheticError(codes);\n}\n\nexport const isThis = (error: ApolloError | GraphQLError | undefined) => {\n if(!error) {\n return new PropheticErrorHandled([]);\n }\n const codes = findCodes(error);\n return new PropheticErrorHandled(codes);\n}"; var toNameAndCodeTupleArray = function (entriesArray) { var entries = types_1.toErrorEntries(entriesArray); return Object.keys(entries).map(function (key) { diff --git a/src/commands/ask/createRawFileFromEntries.spec.ts b/src/commands/ask/createRawFileFromEntries.spec.ts index 01966ac..d3e9fef 100644 --- a/src/commands/ask/createRawFileFromEntries.spec.ts +++ b/src/commands/ask/createRawFileFromEntries.spec.ts @@ -80,7 +80,7 @@ const findCodes = (error: ApolloError | GraphQLError): PropheticErrorCode[] => { return [PropheticErrorCode.CodeLessError]; } -export const errorHere = (error: ApolloError | GraphQLError | undefined ) => { +export const thisError = (error: ApolloError | GraphQLError | undefined ) => { if(!error) { return new PropheticError([]); } diff --git a/src/commands/ask/createRawFileFromEntries.ts b/src/commands/ask/createRawFileFromEntries.ts index 5ac109a..64ec33b 100644 --- a/src/commands/ask/createRawFileFromEntries.ts +++ b/src/commands/ask/createRawFileFromEntries.ts @@ -59,7 +59,7 @@ const findCodes = (error: ApolloError | GraphQLError): PropheticErrorCode[] => { return [PropheticErrorCode.CodeLessError]; } -export const errorHere = (error: ApolloError | GraphQLError | undefined ) => { +export const thisError = (error: ApolloError | GraphQLError | undefined ) => { if(!error) { return new PropheticError([]); }