Store log and send it to slack

Viewed 20

I need to store errors in the logs table. If I understood correctly, I should write a report for this in Handler.ts

This is my Handlet.ts

export default class ExceptionHandler extends HttpExceptionHandler {
  constructor() {
    super(Logger)
  }

  public async handle(error: any, ctx: HttpContextContract) {
    console.log(error.messages)
    await Log.create({
      type: 'menus.store', // if we get error in menu store function
      message: error.messages,
    })

    return super.handle(error, ctx)
  }
}

I have two problems. how can I know in which method error I get? to add it to type in log creation. The second question is that I get the same error that am getting in insomnia when testing. for example:

{"errors":[{"rule":"number","field":"menuId","message":"number validation failed on menuId"},{"rule":"exists","field":"menuId","message":"exists validation failed on menuId"}]}

But I need a full error, I'm getting full error in the visual studio terminal.

How can I add this error to the Log message?

FATAL [17-09-2022, 9:07:53 AM] (projectName/30718 on system): "exists" validation rule failed
    err: {
      "type": "DatabaseError",
      "message": "select 1 from \"menus\" where \"id\" = $1 limit $2 - invalid input syntax for type integer: \"sd\"",
      "stack":
          error: select 1 from "menus" where "id" = $1 limit $2 - invalid input syntax for type integer: "sd"
              at Parser.parseErrorMessage (/home/biping/Development/projectName/backend/node_modules/pg-protocol/src/parser.ts:369:69)
              at Parser.handlePacket (/home/biping/Development/projectName/backend/node_modules/pg-protocol/src/parser.ts:188:21)
              at Parser.parse (/home/biping/Development/projectName/backend/node_modules/pg-protocol/src/parser.ts:103:30)
              at Socket.<anonymous> (/home/biping/Development/projectName/backend/node_modules/pg-protocol/src/index.ts:7:48)
              at Socket.emit (node:events:513:28)
              at addChunk (node:internal/streams/readable:315:12)
              at readableAddChunk (node:internal/streams/readable:289:9)
              at Socket.Readable.push (node:internal/streams/readable:228:10)
              at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
              at TCP.callbackTrampoline (node:internal/async_hooks:130:17)
      "length": 103,
      "name": "error",
      "severity": "ERROR",
      "code": "22P02",
      "file": "numutils.c",
      "line": "256",
      "routine": "pg_strtoint32"
    }

How can I log this error?

0 Answers
Related