Skip to content

Commit

Permalink
Merge pull request #171 from dreamit-de/170-prepending-blank-space
Browse files Browse the repository at this point in the history
170 trim log message
  • Loading branch information
sgohlke authored Jun 2, 2023
2 parents 5d1dac9 + 9f23c41 commit 14eb1c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dreamit/graphql-server",
"version": "4.2.0",
"version": "4.2.1",
"description": "A GraphQL server written in NodeJS/Typescript.",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/logger/CreateLogEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function createLogEntry(logEntryInput: LogEntryInput): LogEntry {

if (error) {
logEntry.errorName = customErrorName ?? error.name
logEntry.message = `${logEntry.message} ${sanitizeMessage(error.message)}`
logEntry.message = `${logEntry.message} ${sanitizeMessage(error.message)}`.trim()
if (error.stack) {
logEntry.stacktrace = sanitizeMessage(error.stack)
}
Expand Down
22 changes: 16 additions & 6 deletions tests/logger/CreateLogEntry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ class FetchError extends Error {
}
}

const customerMessage = '`CustomerPayload` is an extension type'
const customerQuery = '{\n customer {\n dateOfBirth\n }\n}'
const messageWithVariables = 'Variable \\"$login\\" got invalid value' +
' { email: \\"[email protected]\\", password: \\"12345678\\", abc: \\"def\\" }' +
'; Field \\"abc\\" is not defined by type LoginInput.'

const graphQLError: GraphQLError = new GraphQLError('`CustomerPayload` is an extension type', {
const graphQLError: GraphQLError = new GraphQLError(customerMessage, {
extensions: {exception: 'A stacktrace', query: customerQuery, serviceName: 'customer'}
})
const graphQLErrorWithVariables: GraphQLError = new GraphQLError(messageWithVariables, {extensions: {exception: 'A stacktrace', query: customerQuery, serviceName: 'myTestService'}})
const graphQLErrorWithSourceBody = new GraphQLError('`CustomerPayload` is an extension type', {
const graphQLErrorWithSourceBody = new GraphQLError(customerMessage, {
extensions: {
exception: 'A stacktrace',
serviceName: 'customer'
Expand All @@ -45,7 +46,7 @@ const graphQLErrorWithSourceBody = new GraphQLError('`CustomerPayload` is an ext
}
})

const graphQLErrorWithAstNode = new GraphQLError('`CustomerPayload` is an extension type', {
const graphQLErrorWithAstNode = new GraphQLError(customerMessage, {
extensions: {
exception: 'A stacktrace',
serviceName: 'customer'
Expand All @@ -60,7 +61,7 @@ const graphQLErrorWithAstNode = new GraphQLError('`CustomerPayload` is an extens
}
})

const graphQLErrorWithSensibleStacktrace = new GraphQLError('`CustomerPayload` is an extension type', {
const graphQLErrorWithSensibleStacktrace = new GraphQLError(customerMessage, {
extensions: {
exception: messageWithVariables,
serviceName: 'customer'
Expand All @@ -76,15 +77,15 @@ const graphQLErrorWithSensibleStacktrace = new GraphQLError('`CustomerPayload` i
})

const errorWithSensibleStackInformation: Error = {
message: '`CustomerPayload` is an extension type',
message: customerMessage,
name: 'SensibleError',
stack: messageWithVariables
}

const fetchError = new FetchError('An error occurred while connecting to following endpoint',
'system')

const graphQLErrorMessage = 'A GraphQLError message `CustomerPayload` is an extension type'
const graphQLErrorMessage = 'A GraphQLError message ' + customerMessage
const fetchErrorMessage = 'A FetchError message ' +
'An error occurred while connecting to following endpoint'
const sanitizedMessage = 'Variable \\"$login\\" got invalid value REMOVED BY SANITIZER; Field \\"abc\\" is not defined by type LoginInput.'
Expand Down Expand Up @@ -165,3 +166,12 @@ test('Should use fallback values for loggerName, serviceName and level if they a
expect(logEntry.logger).toBe('fallback-logger')
expect(logEntry.serviceName).toBe('fallback-service')
})

test('Should remove white spaces at the beginning of the message', () => {
const logEntry = createLogEntry({
context: undefined,
error: graphQLError,
logMessage: '',
})
expect(logEntry.message).toBe(customerMessage)
})

0 comments on commit 14eb1c0

Please sign in to comment.