在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):thebigredgeek/apollo-errors开源软件地址(OpenSource Url):https://github.com/thebigredgeek/apollo-errors开源编程语言(OpenSource Language):JavaScript 50.3%开源软件介绍(OpenSource Introduction):apollo-errorsMachine-readable custom errors for Apollostack's GraphQL server Example from Apollo DayInstallation and usageInstall the package: npm install apollo-errors Create some errors: import { createError } from 'apollo-errors';
export const FooError = createError('FooError', {
message: 'A foo error has occurred'
}); Hook up formatting: import express from 'express';
import bodyParser from 'body-parser';
import { formatError } from 'apollo-errors';
import { graphqlExpress } from 'apollo-server-express';
import schema from './schema';
const app = express();
app.use('/graphql',
bodyParser.json(),
graphqlExpress({
formatError,
schema
})
);
app.listen(8080) Throw some errors: import { FooError } from './errors';
const resolverThatThrowsError = (root, params, context) => {
throw new FooError({
data: {
something: 'important'
},
internalData: {
error: `The SQL server died.`
}
});
} Witness glorious simplicity:
{
"data": {},
"errors": [
{
"message":"A foo error has occurred",
"name":"FooError",
"time_thrown":"2016-11-11T00:40:50.954Z",
"data":{
"something": "important"
}
}
]
} The import { isInstance as isApolloErrorInstance, formatError as formatApolloError } from 'apollo-errors';
function formatError(error) {
const { originalError } = error;
if (isApolloErrorInstance(originalError)) {
// log internalData to stdout but not include it in the formattedError
console.log(JSON.stringify({
type: `error`,
data: originalError.data,
internalData: originalError.internalData
}));
}
return formatApolloError(error)
} APIApolloError ({ [time_thrown: String, data: Object, internalData: object, message: String ]})Creates a new ApolloError object. Note that createError(name, {message: String, [data: Object, internalData: object, options: Object]}): ApolloErrorCreates and returns an error class with the given Options (default):
formatError (error, strict = false): ApolloError|Error|nullIf the error is a known ApolloError, returns the serialized form of said error. Otherwise, if strict is not truthy, returns the original error passed into formatError. Otherwise, if strict is truthy, returns null. isInstance (error): BooleanReturns true if the error is an instance of an ApolloError. Otherwise, returns false |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论