-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from apollostack/unify_errors
[Breaking change] Unify error handling
- Loading branch information
Showing
9 changed files
with
278 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { GraphQLError } from 'graphql'; | ||
|
||
export class ApolloError extends Error { | ||
public message: string; | ||
public graphQLErrors: GraphQLError[]; | ||
public networkError: Error; | ||
|
||
// Constructs an instance of ApolloError given a GraphQLError | ||
// or a network error. Note that one of these has to be a valid | ||
// value or the constructed error will be meaningless. | ||
constructor({ | ||
graphQLErrors, | ||
networkError, | ||
errorMessage, | ||
}: { | ||
graphQLErrors?: GraphQLError[], | ||
networkError?: Error, | ||
errorMessage?: string, | ||
}) { | ||
super(errorMessage); | ||
this.graphQLErrors = graphQLErrors; | ||
this.networkError = networkError; | ||
|
||
if (!errorMessage) { | ||
this.generateErrorMessage(); | ||
} else { | ||
this.message = errorMessage; | ||
} | ||
} | ||
|
||
// Sets the error message on this error according to the | ||
// the GraphQL and network errors that are present. | ||
// If the error message has already been set through the | ||
// constructor or otherwise, this function is a nop. | ||
private generateErrorMessage() { | ||
if (typeof this.message !== 'undefined' && | ||
this.message !== '') { | ||
return; | ||
} | ||
|
||
let message = ''; | ||
// If we have GraphQL errors present, add that to the error message. | ||
if (Array.isArray(this.graphQLErrors) && this.graphQLErrors.length !== 0) { | ||
this.graphQLErrors.forEach((graphQLError) => { | ||
message += 'GraphQL error: ' + graphQLError.message + '\n'; | ||
}); | ||
} | ||
|
||
if (this.networkError) { | ||
message += 'Network error: ' + this.networkError.message + '\n'; | ||
} | ||
|
||
// strip newline from the end of the message | ||
message = message.replace(/\n$/, ''); | ||
this.message = message; | ||
} | ||
} |
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
Oops, something went wrong.