Provide friendlier error messages for [endpoint] (#2858)

Ref https://github.com/badges/shields/issues/2838#issuecomment-456594803
This commit is contained in:
Paul Melnikow
2019-01-23 18:43:38 -05:00
committed by GitHub
parent a8bedce8a4
commit 4e9763b4c3
5 changed files with 36 additions and 10 deletions

View File

@@ -515,11 +515,20 @@ class BaseService {
)
}
static _validate(data, schema, { allowAndStripUnknownKeys = true } = {}) {
static _validate(
data,
schema,
{
prettyErrorMessage = 'invalid response data',
includeKeys = false,
allowAndStripUnknownKeys = true,
} = {}
) {
return validate(
{
ErrorClass: InvalidResponse,
prettyErrorMessage: 'invalid response data',
prettyErrorMessage,
includeKeys,
traceErrorMessage: 'Response did not match schema',
traceSuccessMessage: 'Response after validation',
allowAndStripUnknownKeys,

View File

@@ -38,7 +38,7 @@ function validate(
if (includeKeys) {
const keys = error.details.map(({ path }) => path)
if (keys) {
prettyMessage = `${prettyErrorMessage}: ${keys.join(',')}`
prettyMessage = `${prettyErrorMessage}: ${keys.join(', ')}`
}
}

View File

@@ -89,7 +89,9 @@ describe('validate', function() {
try {
validate(
{ ...options, includeKeys: true },
{ requiredString: ['this', "shouldn't", 'work'] },
{
requiredString: ['this', "shouldn't", 'work'],
},
schema
)
expect.fail('Expected to throw')
@@ -107,12 +109,19 @@ describe('validate', function() {
})
it('allowAndStripUnknownKeys', function() {
expect(() =>
try {
validate(
{ ...options, allowAndStripUnknownKeys: false },
{ requiredString: 'bar', extra: 'nonsense' },
{ ...options, allowAndStripUnknownKeys: false, includeKeys: true },
{ requiredString: 'bar', extra: 'nonsense', more: 'bogus' },
schema
)
).to.throw(InvalidParameter, '"extra" is not allowed')
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: "extra" is not allowed. "more" is not allowed'
)
expect(e.prettyMessage).to.equal(`${prettyErrorMessage}: extra, more`)
}
})
})

View File

@@ -123,6 +123,8 @@ module.exports = class Endpoint extends BaseJsonService {
})
// Override the validation options because we want to reject unknown keys.
const validated = this.constructor._validate(json, endpointSchema, {
prettyErrorMessage: 'invalid properties',
includeKeys: true,
allowAndStripUnknownKeys: false,
})

View File

@@ -146,7 +146,10 @@ t.create('Invalid schema (mocked)')
schemaVersion: -1,
})
)
.expectJSON({ name: 'custom badge', value: 'invalid response data' })
.expectJSON({
name: 'custom badge',
value: 'invalid properties: schemaVersion',
})
t.create('Invalid schema (mocked)')
.get('.json?url=https://example.com/badge')
@@ -161,7 +164,10 @@ t.create('Invalid schema (mocked)')
bogus: true,
})
)
.expectJSON({ name: 'custom badge', value: 'invalid response data' })
.expectJSON({
name: 'custom badge',
value: 'invalid properties: extra, bogus',
})
t.create('User color overrides success color')
.get('.json?url=https://example.com/badge&colorB=101010&style=_shields_test')