graphql
Version:
A Query Language and Runtime which can target any service.
76 lines (61 loc) • 2 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports.formatError = formatError;
var _utilsInvariant = require('../utils/invariant');
var _utilsInvariant2 = _interopRequireDefault(_utilsInvariant);
var _language = require('../language');
var GraphQLError = function GraphQLError(message,
// A flow bug keeps us from declaring nodes as an array of Node
nodes, stack) {
_classCallCheck(this, GraphQLError);
this.message = message;
this.stack = stack || message;
if (nodes) {
this.nodes = nodes;
var positions = nodes.map(function (node) {
return node.loc && node.loc.start;
});
if (positions.some(function (p) {
return !!p;
})) {
this.positions = positions;
var loc = nodes[0].loc;
var source = loc && loc.source;
if (source) {
this.locations = positions.map(function (pos) {
return (0, _language.getLocation)(source, pos);
});
this.source = source;
}
}
}
};
exports.GraphQLError = GraphQLError;
GraphQLError.prototype = Error.prototype;
function formatError(error) {
(0, _utilsInvariant2['default'])(error, 'Received null or undefined error.');
if (error.locations) {
return {
message: error.message || '' + error,
locations: error.locations || null
};
}
return {
message: error.message || '' + error
};
}
/*Node*/