简体   繁体   中英

How can I access this “hidden” object key in a feathersjs HookContext.error?

I'm trying to check when feathersjs throws an error containing data about the DB so I can prevent it from showing up on the client side. I have figured out that in the custom error handler function, which takes a HookContext as an arg, when the error is DB related, the context.error object contains an additional field denoted as [Symbol(feathers-knex/error)] .

Here is what console.log(context.error) looks like:

{
  type: 'FeathersError',
  name: 'BadRequest',
  message: "insert into `users` (`created_by_uid`, `id`) values (DEFAULT, NaN) - Unknown column 'NaN' in 'field list'",
  code: 400,
  className: 'bad-request',
  data: undefined,
  errors: {},
  hook: null,
  [Symbol(feathers-knex/error)]: Error: Unknown column 'NaN' in 'field list'
      at Packet.asError (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/packets/packet.js:708:17)
      at Query.execute (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/commands/command.js:28:26)
      at Connection.handlePacket (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/connection.js:408:32)
      at PacketParser.onPacket (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/connection.js:70:12)
      at PacketParser.executeStart (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/packet_parser.js:75:16)
      at TLSSocket.<anonymous> (/Users/matthewwolfe/vretta/mpt-api/node_modules/mysql2/lib/connection.js:328:25)
      at TLSSocket.emit (events.js:305:20)
      at TLSSocket.EventEmitter.emit (domain.js:483:12)
      at addChunk (_stream_readable.js:341:12)
      at readableAddChunk (_stream_readable.js:316:11)
      at TLSSocket.Readable.push (_stream_readable.js:250:10)
      at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23) {
    code: 'ER_BAD_FIELD_ERROR',
    errno: 1054,
    sqlState: '42S22',
    sqlMessage: "Unknown column 'NaN' in 'field list'"
  }
}

The issue I'm having is with trying to access this field. When I call Object.keys(context.error) this other field I'm looking for does not show up. Only 8 keys show up, the ones above what I'm looking for.

I've tried doing context.error[Symbol('feathers-knex/error')] but this comes up as undefined. I imagine this might have something to do with the square brackets around the key?

How can I access this field?

We are using feathers-knex to interface with our MySQL database. As per their docs ,

As of version 4.0.0 feathers-knex only throws Feathers Errors with the message. On the server, the original error can be retrieved through a secure symbol via error[require('feathers-knex').ERROR]

So calling context.error[require('feathers-knex').ERROR] allowed me to access the field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM