Skip to content

Commit 7c104f3

Browse files
Alan-ChaErikWittern
authored andcommitted
Add extensions for errors
Fix #115 Signed-off-by: Alan Cha <[email protected]>
1 parent 59257d5 commit 7c104f3

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

packages/oasgraph/lib/resolver_builder.js

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/resolver_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/src/resolver_builder.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as Oas3Tools from './oas_3_tools'
1919
import * as querystring from 'querystring'
2020
import * as JSONPath from 'jsonpath-plus'
2121
import { debug } from 'debug'
22+
import { GraphQLError } from 'graphql'
2223

2324
const log = debug('http')
2425
// Type definitions & exports:
@@ -267,7 +268,18 @@ export function getResolver({
267268
reject(err)
268269
} else if (response.statusCode > 299) {
269270
log(`${response.statusCode} - ${Oas3Tools.trim(body, 100)}`)
270-
reject(new Error(`${response.statusCode} - ${JSON.stringify(body)}`))
271+
272+
const operationString = `${operation.method.toUpperCase()} ${operation.path}`
273+
const extensions = {
274+
method: operation.method,
275+
path: operation.path,
276+
277+
statusCode: response.statusCode,
278+
responseHeaders: response.headers,
279+
responseBody: JSON.parse(body)
280+
}
281+
reject(graphQLErrorWithExtensions(`Could not invoke operation ${operationString}`, extensions))
282+
271283
} else {
272284
log(`${response.statusCode} - ${Oas3Tools.trim(body, 100)}`)
273285

@@ -618,10 +630,17 @@ function getIdentifier(info): string {
618630
function getParentIdentifier(info): string {
619631
return getIdentifierRecursive(info.path.prev)
620632
}
621-
633+
622634
/**
623635
* Get the path of nested field names (or aliases if provided)
624636
*/
625637
function getIdentifierRecursive(path): string {
626638
return (typeof path.prev === 'undefined') ? path.key : `${path.key}/${getIdentifierRecursive(path.prev)}`
627639
}
640+
641+
/**
642+
* Create a new GraphQLError with an extensions field
643+
*/
644+
function graphQLErrorWithExtensions(message: string, extensions: { [key: string]: any }): GraphQLError {
645+
return new GraphQLError(message, null, null, null, null, null, extensions)
646+
}

packages/oasgraph/test/example_api.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,3 +673,26 @@ test('Resolve allOf', () => {
673673
})
674674
})
675675
})
676+
677+
test('Error contains extension', () => {
678+
let query = `query {
679+
user(username: "abcdef") {
680+
name
681+
}
682+
}`
683+
return graphql(createdSchema, query, null, {}).then(error => {
684+
const extensions = error.errors[0].extensions
685+
expect(extensions).toBeDefined()
686+
687+
// Remove headers because it contains fields that may change from run to run
688+
delete extensions.responseHeaders
689+
expect(extensions).toEqual({
690+
"method": "get",
691+
"path": "/users/{username}",
692+
"statusCode": 401,
693+
"responseBody": {
694+
"message": "Wrong username."
695+
}
696+
})
697+
})
698+
})

packages/oasgraph/test/example_api_server.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,13 @@ function startServer (PORT) {
278278

279279
app.get('/api/users/:username', (req, res) => {
280280
console.log(req.method, req.path)
281-
res.send(Users[req.params.username])
281+
if (req.params.username in Users) {
282+
res.send(Users[req.params.username])
283+
} else {
284+
res.status(401).send({
285+
message: 'Wrong username.'
286+
})
287+
}
282288
})
283289

284290
app.get('/api/users/:username/car', (req, res) => {

packages/oasgraph/test/example_gql_server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const graphqlHTTP = require('express-graphql')
1010
const app = express()
1111
const OasGraph = require('../lib/index.js')
1212

13-
// let oas = require('./fixtures/example_oas.json')
13+
let oas = require('./fixtures/example_oas.json')
1414
// let oas2 = require('./fixtures/example_oas2.json')
1515
// let oas3 = require('./fixtures/example_oas3.json')
1616

1717
// let oas = require('./fixtures/github_oas.json')
1818
// let oas = require('./fixtures/instagram.json')
19-
let oas = require('./fixtures/government_social_work_api.json')
19+
// let oas = require('./fixtures/government_social_work_api.json')
2020
// let oas = require('./fixtures/weather_underground_api.json')
2121

2222
// const yamljs = require('yamljs')

0 commit comments

Comments
 (0)