Skip to content

Commit

Permalink
adjust tests for revised log formatting in production (#138)
Browse files Browse the repository at this point in the history
* adjust tests for revised log formatting in production

* Add error logging tests for dev

---------

Co-authored-by: Marcel Schwarz <[email protected]>
  • Loading branch information
sjvans and schwma authored Nov 28, 2023
1 parent d792ed1 commit 136d950
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
64 changes: 64 additions & 0 deletions test/tests/error-handling-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ describe('graphql - error handling in development', () => {
// Prevent axios from throwing errors for non 2xx status codes
axios.defaults.validateStatus = false

beforeEach(() => {
jest.spyOn(console, 'warn')
jest.spyOn(console, 'error')
})

afterEach(() => {
jest.clearAllMocks()
})

describe('Errors thrown by CDS', () => {
test('Single @mandatory validation error', async () => {
const query = gql`
Expand Down Expand Up @@ -175,6 +184,38 @@ describe('graphql - error handling in development', () => {
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(response.data.errors[0].extensions.details[0].stacktrace[0]).not.toHaveLength(0) // Stacktrace exists and is not empty
expect(response.data.errors[0].extensions.details[1].stacktrace[0]).not.toHaveLength(0) // Stacktrace exists and is not empty
expect(console.warn.mock.calls[0][1]).toMatchObject({
code: '400',
message: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
args: ['inRange'],
code: '400',
element: 'inRange',
entity: 'ValidationErrorsService.C',
message: 'Value is required',
numericSeverity: 4,
target: 'inRange',
type: 'cds.Integer',
value: undefined,
stack: expect.any(String)
},
{
args: ['"foo"', '"high", "medium", "low"'],
code: '400',
element: 'oneOfEnumValues',
entity: 'ValidationErrorsService.C',
enum: ['@assert.range', 'type', 'enum'],
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}',
numericSeverity: 4,
target: 'oneOfEnumValues',
type: 'cds.String',
value: 'foo',
stack: expect.any(String)
}
]
})
expect(console.warn.mock.calls[0][1]).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})
})

Expand Down Expand Up @@ -361,6 +402,29 @@ describe('graphql - error handling in development', () => {
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(response.data.errors[0].extensions.details[0].stacktrace[0]).not.toHaveLength(0) // Stacktrace exists and is not empty
expect(response.data.errors[0].extensions.details[1].stacktrace[0]).not.toHaveLength(0) // Stacktrace exists and is not empty
expect(console.error.mock.calls[0][1]).toMatchObject({
code: '500',
message: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
code: 'Some-Custom-Code1',
message: 'Some Custom Error Message 1',
numericSeverity: 4,
status: 418,
target: 'some_field',
stack: expect.any(String)
},
{
code: 'Some-Custom-Code2',
message: 'Some Custom Error Message 2',
numericSeverity: 4,
status: 500,
target: 'some_field',
stack: expect.any(String)
}
]
})
expect(console.error.mock.calls[0][1]).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})

test('Thrown error is modified in srv.on(error) handler', async () => {
Expand Down
36 changes: 17 additions & 19 deletions test/tests/error-handling-prod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ describe('graphql - error handling in production', () => {
const response = await POST('/graphql', { query })
expect(response.data).toMatchObject({ errors })
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace in production
expect(console.warn.mock.calls[0][1]).toMatchObject({
const log = console.warn.mock.calls[0][1] || JSON.parse(console.warn.mock.calls[0][0])
expect(log).toMatchObject({
code: '400',
element: 'notEmptyI',
entity: 'ValidationErrorsService.A',
message: 'Value is required',
numericSeverity: 4,
target: 'notEmptyI',
type: 'cds.Integer',
value: undefined,
stack: expect.any(String)
type: 'cds.Integer'
})
})

Expand Down Expand Up @@ -157,9 +156,11 @@ describe('graphql - error handling in production', () => {
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(response.data.errors[0].extensions.details[0]).not.toHaveProperty('stacktrace') // No stacktrace in production
expect(response.data.errors[0].extensions.details[1]).not.toHaveProperty('stacktrace') // No stacktrace in production
expect(console.warn.mock.calls[0][1]).toMatchObject({
const log = console.warn.mock.calls[0][1] || JSON.parse(console.warn.mock.calls[0][0])
const msgProperty = log.msg ? 'msg' : 'message'
expect(log).toMatchObject({
code: '400',
message: 'Multiple errors occurred. Please see the details for more information.',
[msgProperty]: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
args: ['inRange'],
Expand All @@ -169,9 +170,7 @@ describe('graphql - error handling in production', () => {
message: 'Value is required',
numericSeverity: 4,
target: 'inRange',
type: 'cds.Integer',
value: undefined,
stack: expect.any(String)
type: 'cds.Integer'
},
{
args: ['"foo"', '"high", "medium", "low"'],
Expand All @@ -183,12 +182,11 @@ describe('graphql - error handling in production', () => {
numericSeverity: 4,
target: 'oneOfEnumValues',
type: 'cds.String',
value: 'foo',
stack: expect.any(String)
value: 'foo'
}
]
})
expect(console.warn.mock.calls[0][1]).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(log).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})
})

Expand Down Expand Up @@ -362,29 +360,29 @@ describe('graphql - error handling in production', () => {
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(response.data.errors[0].extensions.details[0]).not.toHaveProperty('stacktrace') // No stacktrace in production
expect(response.data.errors[0].extensions.details[1]).not.toHaveProperty('stacktrace') // No stacktrace in production
expect(console.error.mock.calls[0][1]).toMatchObject({
const log = console.error.mock.calls[0][1] || JSON.parse(console.error.mock.calls[0][0])
const msgProperty = log.msg ? 'msg' : 'message'
expect(log).toMatchObject({
code: '500',
message: 'Multiple errors occurred. Please see the details for more information.',
[msgProperty]: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
code: 'Some-Custom-Code1',
message: 'Some Custom Error Message 1',
numericSeverity: 4,
status: 418,
target: 'some_field',
stack: expect.any(String)
target: 'some_field'
},
{
code: 'Some-Custom-Code2',
message: 'Some Custom Error Message 2',
numericSeverity: 4,
status: 500,
target: 'some_field',
stack: expect.any(String)
target: 'some_field'
}
]
})
expect(console.error.mock.calls[0][1]).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
expect(log).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})

test('Thrown error is modified in srv.on(error) handler', async () => {
Expand Down

0 comments on commit 136d950

Please sign in to comment.