Vuex: Vuex should throw exception on missing mutation type

Created on 5 Nov 2019  路  3Comments  路  Source: vuejs/vuex

Version

3.1.1

Reproduction link

https://codesandbox.io/s/vue-template-2tpsf

Steps to reproduce

  1. Create a new vuex store
  2. From an action, commit a mutation that doesn't exist
  3. Call the action

What is expected?

For an exception to be thrown.

What is actually happening?

An exception is not thrown, instead a console.error is logged, and only if we're not running in production. If we're running in production, literally nothing happens:

    if (process.env.NODE_ENV !== 'production') {
      console.error(("[vuex] unknown mutation type: "   type));
    }

In the sandbox link above, you can see there is no exception is caught because AN ERROR WAS CAUGHT! should appear in the console log, but it doesn't, per:

      try {
        commit("fo/bar");
      } catch (e) {
        console.error("AN ERROR WAS CAUGHT!", e);
      }

Our error handling code cannot function correctly if no exception is thrown (as it should be, in this case).

All 3 comments

This check is meant to be notice the developers that there is a mistake on their code (which is assertion) and only available in development env. Also we strip out it on production env to reduce the file size.
You should not rely on the behavior when you implement your app logic.

Can this issue be reopened? It's causing serious problems for us. Sometimes the error happens for some users, and sometimes it doesn't. Without being able to catch this there is no way for us to handle this (EDIT: without manually checking store._mutations, which I assume is internal API that shouldn't be touched, but this hack is what we're forced to resort to...)

I also think this should throw an error.

The console message could be easily missed during development, and if code goes to production it will fail silently, making it very hard to debug issues.

Is there ever a valid case for having a dispatch or commit refer to an action that doesn't exist? Otherwise, why not throw an error??

Was this page helpful?
0 / 5 - 0 ratings