Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear CastError #19

Closed
Fabian-Fynn opened this issue May 10, 2018 · 5 comments
Closed

Unclear CastError #19

Fabian-Fynn opened this issue May 10, 2018 · 5 comments

Comments

@Fabian-Fynn
Copy link

Fabian-Fynn commented May 10, 2018

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
Copy link
Owner

can you provide the exact error message?

@Fabian-Fynn
Copy link
Author

Fabian-Fynn commented May 10, 2018

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
Copy link
Owner

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

@Fabian-Fynn
Copy link
Author

Fabian-Fynn commented May 10, 2018

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants