aichbauer / express-routes-mapper

a small mapper for express routes
MIT License
21 stars 16 forks source link

Unclear CastError #19

Closed Fabian-Fynn closed 6 years ago

Fabian-Fynn commented 6 years ago

When there are two routes with a similar path but one has an ID and is before the other one the resulting CastError is confusing

'DELETE /user/:userId': 'adminController.destroyUser', 
'DELETE /user/remove-provisional': 'adminController.removeProvisional',
aichbauer commented 6 years ago

can you provide the exact error message?

Fabian-Fynn commented 6 years ago

Sure remove-provisional is interpreted as :userId, which is expected but the error could inform what's wrong

{ CastError: Cast to ObjectId failed for value "remove-provisional" at path "_id" for model "User"
    at new CastError (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/error/cast.js:27:11)
    at ObjectId.cast (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/schema/objectid.js:158:13)
    at ObjectId.SchemaType.applySetters (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/schematype.js:724:12)
    at ObjectId.SchemaType._castForQuery (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/schematype.js:1095:15)
    at ObjectId.castForQuery (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/schema/objectid.js:198:15)
    at ObjectId.SchemaType.castForQueryWrapper (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/schematype.js:1064:15)
    at cast (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/cast.js:300:32)
    at model.Query.Query.cast (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/query.js:3216:12)
    at model.Query.Query._castConditions (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/query.js:1286:10)
    at model.Query.Query._findOne (/Users/Fabi/Sites/node-project-boilerplate/node_modules/mongoose/lib/query.js:1502:8)
    at process.nextTick (/Users/Fabi/Sites/node-project-boilerplate/node_modules/kareem/index.js:315:33)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
  message: 'Cast to ObjectId failed for value "remove-provisional" at path"_id" for model "User"',
  name: 'CastError',
  stringValue: '"remove-provisional"',
  kind: 'ObjectId',
  value: 'remove-provisional',
  path: '_id',
  reason: undefined,
  model:
   { [Function: model]
     hooks: Kareem { _pres: [Object], _posts: [Object] },
     base:
      Mongoose {
        connections: [Array],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        _pluralize: [Function: pluralize],
        plugins: [Array] },
     modelName: 'User',
     model: [Function: model],
     db:
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        options: null,
        otherDbs: [],
        relatedDbs: {},
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        _connectionOptions: [Object],
        client: [Object],
        name: 'projectName',
        '$initialConnection': [Object],
        db: [Object] },
     discriminators: undefined,
     '$appliedMethods': true,
     '$appliedHooks': true,
     schema:
      Schema {
        obj: [Object],
        paths: [Object],
        aliases: {},
        subpaths: {},
        virtuals: [Object],
        singleNestedPaths: {},
        nested: [Object],
        inherits: {},
        callQueue: [],
        _indexes: [],
        methods: [Object],
        statics: {},
        tree: [Object],
        query: {},
        childSchemas: [],
        plugins: [Array],
        s: [Object],
        _userProvidedOptions: [Object],
        options: [Object],
        '$globalPluginsApplied': true,
        _requiredpaths: [],
        _indexedpaths: [Array] },
     collection:
      NativeCollection {
        collection: [Object],
        opts: [Object],
        name: 'users',
        collectionName: 'users',
        conn: [Object],
        queue: [],
        buffer: false,
        emitter: [Object] },
     Query: { [Function] base: [Object] },
     '$__insertMany': [Function],
     '$init': Promise { [Circular], catch: [Function] } } }
aichbauer commented 6 years ago

But this is a error message from mongoose. So there is nothing that can be done here. Or what do you mean?

Fabian-Fynn commented 6 years ago

Well this error is thrown when mongoose tries to cast remove-provisional to ObjectId Maybe it's possible to catch that error and suggest what the problem could be Or throw an error when a route /model/action is found after a /model/:modelId route

aichbauer commented 6 years ago

But we cannot implement error messages for another library... if message: 'Cast to ObjectId failed for value "remove-provisional" at path"_id" for model "User"', has too less information, that should be added in mongoose.