Angular - ErrorHandler Help

60 views
Skip to first unread message

Marcus Malcom

unread,
Jan 2, 2021, 12:18:41 PM1/2/21
to Angular and AngularJS discussion
I originally posted this question here: Angular Amplify Error Handler - GraphQL API Calls. The basics are:
  1. I have a global error handler that is working for calls to an API service
  2. When I do a try/catch, I get an error object that data and errors as members (the catch is getting an object of this type: GraphQLResult)
  3. The error object I receive in my error handler is not a GraphQLResult. Instead it is an error object that has an error message, GraphQLResult converted to a string and stack trace
I'm trying to figure out if there's a better way to do this, any help?

Thank you!

Marcus Malcom

unread,
Jan 7, 2021, 3:22:39 PM1/7/21
to Angular and AngularJS discussion
So this might have been super obvious to other folks out there but it wasn't for me. You can get at the original GraphQLResult by looking at the error.promise and error.rejection. So in your handleError method:

---------------------
isGraphQLResult(rejection: any): boolean {
  return rejection !== undefined && (rejection.data !== undefined || rejection.errors !== undefined);
}

handleError(error: any): void {

  if (error !== undefined && this.isGraphQLResult(error.rejection)) {
    const graphqlResult: GraphQLResult = error.rejection as GraphQLResult;
    // handle the graphqlResult
  }
}
---------------------

Honestly didn't know that error.promise and error.rejection were even there ... I guess I missed the memo on that one.


Reply all
Reply to author
Forward
0 new messages