Payload uses `pino` for a logger. When using the error logger
`payload.logger.error` it is possible to pass any number of arguments
like this: `payload.logger.error('Some error ocurred', err)`. However,
in this scenario, the full error will not be serialized by `pino`. It
must be passed as the `err` property inside of an object in order to be
properly serialized.
This rule ensures that a user is using this function call to properly serialize the error.
73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
import { RuleTester } from 'eslint'
|
|
import rule from '../customRules/proper-payload-logger-usage.js'
|
|
|
|
const ruleTester = new RuleTester()
|
|
|
|
// Example tests for the rule
|
|
ruleTester.run('no-improper-payload-logger-error', rule, {
|
|
valid: [
|
|
// Valid: payload.logger.error with object containing { msg, err }
|
|
{
|
|
code: "payload.logger.error({ msg: 'some message', err })",
|
|
},
|
|
// Valid: payload.logger.error with a single string
|
|
{
|
|
code: "payload.logger.error('Some error message')",
|
|
},
|
|
// Valid: *.payload.logger.error with object
|
|
{
|
|
code: "this.payload.logger.error({ msg: 'another message', err })",
|
|
},
|
|
{
|
|
code: "args.req.payload.logger.error({ msg: 'different message', err })",
|
|
},
|
|
],
|
|
|
|
invalid: [
|
|
// Invalid: payload.logger.error with both string and error
|
|
{
|
|
code: "payload.logger.error('Some error message', err)",
|
|
errors: [
|
|
{
|
|
messageId: 'improperUsage',
|
|
},
|
|
],
|
|
},
|
|
// Invalid: *.payload.logger.error with both string and error
|
|
{
|
|
code: "this.payload.logger.error('Some error message', error)",
|
|
errors: [
|
|
{
|
|
messageId: 'improperUsage',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "args.req.payload.logger.error('Some error message', err)",
|
|
errors: [
|
|
{
|
|
messageId: 'improperUsage',
|
|
},
|
|
],
|
|
},
|
|
// Invalid: payload.logger.error with object containing 'message' key
|
|
{
|
|
code: "payload.logger.error({ message: 'not the right property name' })",
|
|
errors: [
|
|
{
|
|
messageId: 'wrongMessageField',
|
|
},
|
|
],
|
|
},
|
|
// Invalid: *.payload.logger.error with object containing 'error' key
|
|
{
|
|
code: "this.payload.logger.error({ msg: 'another message', error })",
|
|
errors: [
|
|
{
|
|
messageId: 'wrongErrorField',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|