apollo-client
Version:
A simple yet functional GraphQL client.
61 lines • 2.62 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
export function isApolloError(err) {
return err.hasOwnProperty('graphQLErrors');
}
// 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.
var generateErrorMessage = function (err) {
var message = '';
// If we have GraphQL errors present, add that to the error message.
if (Array.isArray(err.graphQLErrors) && err.graphQLErrors.length !== 0) {
err.graphQLErrors.forEach(function (graphQLError) {
var errorMessage = graphQLError
? graphQLError.message
: 'Error message not found.';
message += "GraphQL error: " + errorMessage + "\n";
});
}
if (err.networkError) {
message += 'Network error: ' + err.networkError.message + '\n';
}
// strip newline from the end of the message
message = message.replace(/\n$/, '');
return message;
};
var ApolloError = /** @class */ (function (_super) {
__extends(ApolloError, _super);
// 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.
function ApolloError(_a) {
var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError, errorMessage = _a.errorMessage, extraInfo = _a.extraInfo;
var _this = _super.call(this, errorMessage) || this;
_this.graphQLErrors = graphQLErrors || [];
_this.networkError = networkError || null;
if (!errorMessage) {
_this.message = generateErrorMessage(_this);
}
else {
_this.message = errorMessage;
}
_this.extraInfo = extraInfo;
// We're not using `Object.setPrototypeOf` here as it isn't fully
// supported on Android (see issue #3236).
_this.__proto__ = ApolloError.prototype;
return _this;
}
return ApolloError;
}(Error));
export { ApolloError };
//# sourceMappingURL=ApolloError.js.map