-
Notifications
You must be signed in to change notification settings - Fork 2.7k
graphql-client throws Error instance instead of ApolloError instance. #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this behavior still in |
@calebmer I'll try to update to 0.7.3. I do understand the lack of support right now, and as far as my understanding of SemVer goes I think it's ok to have eventual breaking changes while a major version is not released. It is a strange issue though. If I happen to have the same issue with version 0.7.3 I'll report back here and either continue using 0.5.10 or work on a pull-request. Thanks for your feedback. |
Hello! I've upgraded to 0.7.3 and there still persists. It's interesting to note two things about this version:
Here goes the two files I'm talking about, in the TypeScript and JavaScript version, as the second is not available at this repository but only after compiling locally: ./errors/ApolloError.d.ts: /// <reference types="graphql" />
import { GraphQLError } from 'graphql';
export declare function isApolloError(err: Error): err is ApolloError;
export declare class ApolloError extends Error {
message: string;
graphQLErrors: GraphQLError[];
networkError: Error;
extraInfo: any;
constructor({graphQLErrors, networkError, errorMessage, extraInfo}: {
graphQLErrors?: GraphQLError[];
networkError?: Error;
errorMessage?: string;
extraInfo?: any;
});
} ./errors/ApolloError.js: var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
export function isApolloError(err) {
return err.hasOwnProperty('graphQLErrors');
}
var generateErrorMessage = function (err) {
var message = '';
if (Array.isArray(err.graphQLErrors) && err.graphQLErrors.length !== 0) {
err.graphQLErrors.forEach(function (graphQLError) {
message += 'GraphQL error: ' + graphQLError.message + '\n';
});
}
if (err.networkError) {
message += 'Network error: ' + err.networkError.message + '\n';
}
message = message.replace(/\n$/, '');
return message;
};
var ApolloError = (function (_super) {
__extends(ApolloError, _super);
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;
_this.stack = new Error().stack;
if (!errorMessage) {
_this.message = generateErrorMessage(_this);
}
else {
_this.message = errorMessage;
}
_this.extraInfo = extraInfo;
return _this;
}
return ApolloError;
}(Error));
export { ApolloError };
//# sourceMappingURL=ApolloError.js.map |
I just did some research and what’s happening here is kind of interesting. This is not a bug in TypeScript, As you can see in the TypeScript output (and the Babel output) if the constructor function ( function Person (name) {
this.name = name;
}
assert(typeof Person.call({}, 'Caleb') === 'undefined'); However when calling assert(typeof Error.call({}, 'My error message') !== 'undefined'); This means on the line TypeScript builds: var _this = _super.call(this, errorMessage) || this; …the default of So this does appear to be a breaking change in the way TypeScript emits classes. https://unpkg.com/[email protected]/errors/ApolloError.js vs. https://unpkg.com/[email protected]/errors/ApolloError.js. However, it seems like the old TypeScript behavior was incorrect according to the spec. It looks like @helfer added the duck-typing method |
What a lesson you gave me here :) Thanks a lot for your interest in the matter, really. I still think it's quite strange that we can't compare types, for I believe this can happen in quite so many other situations other than what I was trying to do. It's odd, indeed. Well, I guess we can close this issue now :) |
I'm experiencing a slight breaking change from 0.5.10 to 0.5.26. Basically, errors thrown from graphql-client are instances of ApolloError at the first, but not at the later.
Here goes a code to reproduce this issue:
I found this problem doing error normalization using the previous version, but once I've update the module, the code stopped working.
The text was updated successfully, but these errors were encountered: