Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust tests for revised log formatting in production #138

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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