UNPKG

2.93 kBJavaScriptView Raw
1import { __extends, __spreadArray } from "tslib";
2import "../utilities/globals/index.js";
3import { isNonNullObject } from "../utilities/index.js";
4// This Symbol allows us to pass transport-specific errors from the link chain
5// into QueryManager/client internals without risking a naming collision within
6// extensions (which implementers can use as they see fit).
7export var PROTOCOL_ERRORS_SYMBOL = Symbol();
8export function graphQLResultHasProtocolErrors(result) {
9 if (result.extensions) {
10 return Array.isArray(result.extensions[PROTOCOL_ERRORS_SYMBOL]);
11 }
12 return false;
13}
14export function isApolloError(err) {
15 return err.hasOwnProperty("graphQLErrors");
16}
17// Sets the error message on this error according to the
18// the GraphQL and network errors that are present.
19// If the error message has already been set through the
20// constructor or otherwise, this function is a nop.
21var generateErrorMessage = function (err) {
22 var errors = __spreadArray(__spreadArray(__spreadArray([], err.graphQLErrors, true), err.clientErrors, true), err.protocolErrors, true);
23 if (err.networkError)
24 errors.push(err.networkError);
25 return (errors
26 // The rest of the code sometimes unsafely types non-Error objects as GraphQLErrors
27 .map(function (err) {
28 return (isNonNullObject(err) && err.message) || "Error message not found.";
29 })
30 .join("\n"));
31};
32var ApolloError = /** @class */ (function (_super) {
33 __extends(ApolloError, _super);
34 // Constructs an instance of ApolloError given serialized GraphQL errors,
35 // client errors, protocol errors or network errors.
36 // Note that one of these has to be a valid
37 // value or the constructed error will be meaningless.
38 function ApolloError(_a) {
39 var graphQLErrors = _a.graphQLErrors, protocolErrors = _a.protocolErrors, clientErrors = _a.clientErrors, networkError = _a.networkError, errorMessage = _a.errorMessage, extraInfo = _a.extraInfo;
40 var _this = _super.call(this, errorMessage) || this;
41 _this.name = "ApolloError";
42 _this.graphQLErrors = graphQLErrors || [];
43 _this.protocolErrors = protocolErrors || [];
44 _this.clientErrors = clientErrors || [];
45 _this.networkError = networkError || null;
46 _this.message = errorMessage || generateErrorMessage(_this);
47 _this.extraInfo = extraInfo;
48 _this.cause =
49 __spreadArray(__spreadArray(__spreadArray([
50 networkError
51 ], (graphQLErrors || []), true), (protocolErrors || []), true), (clientErrors || []), true).find(function (e) { return !!e; }) || null;
52 // We're not using `Object.setPrototypeOf` here as it isn't fully
53 // supported on Android (see issue #3236).
54 _this.__proto__ = ApolloError.prototype;
55 return _this;
56 }
57 return ApolloError;
58}(Error));
59export { ApolloError };
60//# sourceMappingURL=index.js.map
\No newline at end of file