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

Better error message for: Invariant Violation: [React Intl] An id must be provided to format a message. #1059

Closed
frozenfung opened this issue Nov 3, 2017 · 26 comments

Comments

@frozenfung
Copy link

frozenfung commented Nov 3, 2017

Is this a BUG REPORT or FEATURE REQUEST? (choose one)

Feature.

If this is a FEATURE REQUEST, please:

  • Describe in detail the feature/behavior/change you'd like to see.
    -->

Something I forget implement the default locale or maybe misspell one of them, in this hole I will get a notice like:

Invariant Violation: [React Intl] An id must be provided to format a message.

However, I think the message could be more helpful if we supply exactly what the missing attribute is, right?

p.s. I have not read the code yet so I do not know if this is doable or not, but if it is I am glad to submit a PR for this.

Expected behavior

Supply with a more detailed message, maybe:

Invariant Violation: [React Intl] An 'id' must be provided to format a message for key 'missLocaleKey'.

@papasmile
Copy link
Contributor

Can you be a little more specific about your problem? Locale is set on IntlProvider, not FormatMessage. Usually you get that error when you pass an incorrect message key, so that has nothing to do with locale and is general. E.g., all those will give you a 'missing id' error:

intl.formatMessage({ defaultMessage: 'hi });
intl.formatMessage({ ids: 'xxx.yyy', defaultMessage': 'hi });
intl.formatMessage({ extrakey: { id: 'aaa.bbb', defaultMessage': 'hi } });

Maybe I'm missing which API call you are thinking of and you can share...

@jdolinski1
Copy link

jdolinski1 commented Feb 21, 2018

I think what everybody desires is more detailed Error info when non-existing id is provided to FormattedMessage, intl.formatMessage, etc.

Current error message when something got screwed:
[React Intl] An id must be provided to format a message. - No line number, no parent component name, nothing.

Desired error message:
[React Intl] Error occured in: <ComponentName> An id must be provided to format a message. You provided <id> which was not found in dictionary.

Is this actually doable or am i missing something?

Thanks

@damiangreen
Copy link

damiangreen commented Sep 6, 2018

For anyone that lands here,

this can be caused by your message key not existing in your defineMessages object, can easily happen if you rename your key and not changing it in FormattedMessage.

@justinkahrs
Copy link

Any updates on this? Would be very helpful.

@velidan
Copy link

velidan commented Apr 3, 2019

I'm also have this issue. It happens when I'm trying to define a template string in my "de" locale file.

	creditsMsgTemplate: {
		id: "_creditsMsg",
		defaultMessage:
			"{creditCount} { creditCount, plural, one {{credit}} other {{credits}}}"
	},

I don't need to have ID because I want to have exactly this defaultMessage to interpolate but it looks like there is no any other way to do it without this unnecessary ID.
So I'm receiving error about default fallback using because my DE locale fine has already the key creditsMsgTemplate

It's very annoying.

@stale
Copy link

stale bot commented Jun 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 2, 2019
@justinkahrs
Copy link

This isn't stale, it's just a request that hasn't seen any action.

@stale stale bot removed the wontfix label Jun 2, 2019
@redonkulus
Copy link
Member

The id is used as a lookup to find the message, so as of right now it’s required. If there is no ID supplied it’s hard to know what info to give. I’ve marked this as an enhancement so stalebot doesn’t close it again. But unless someone offers to help this probably will not get resolved.

@pengpeng9413
Copy link

我遇到了同样的问题,但是由于代码的复杂,我并不知道其发生的细节与错误的定位,希望能提示更详细的错误,感谢

@CodeForcer
Copy link

I feel this is a really important issue. Often I'm dealing with huge files and somewhere in there a missing ID causes the page to occasionally crash.

Tracking down which ID is missing is really frustrating and something I regularly deal with. The only solution seems to be extensive manual testing

@longlho
Copy link
Member

longlho commented Jan 4, 2020 via email

@atav32
Copy link

atav32 commented Apr 8, 2020

@longlho curious to know which linter you're referring to 😄

Is it this one https://github.com/calm/eslint-plugin-react-intl?

@longlho
Copy link
Member

longlho commented Apr 8, 2020

This is the one I'm referring to: https://www.npmjs.com/package/eslint-plugin-formatjs :)

@mguida22
Copy link
Contributor

@longlho is it possible to provide some more information on where the issue may be located as @jdolinski1 suggested?

I'd be happy to make a PR with changes!

Current error message when something got screwed:
[React Intl] An id must be provided to format a message. - No line number, no parent component name, nothing.

Desired error message:
[React Intl] Error occured in: <ComponentName> An id must be provided to format a message. You provided <id> which was not found in dictionary.

@longlho
Copy link
Member

longlho commented Jun 10, 2020

Our latest version has a proper stack trace & error code now

@stale
Copy link

stale bot commented Jun 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 25, 2020
@stale stale bot closed this as completed Jul 2, 2020
@slim-hmidi
Copy link

The page hasn't been displayed when I accessed it the first time and I need to refresh it to display the compoents. How can I fix that issue?

@longlho
Copy link
Member

longlho commented Jul 22, 2020

@slim-hmidi please create another GH issue w/ repro link

@TimDaub
Copy link

TimDaub commented Mar 30, 2021

I'm currently getting this message in node and I'm trying to figure out what I'm doing wrong.

const { createIntl, createIntlCache } = require("@formatjs/intl");
const cache = createIntlCache();
const msgObj = {
  locale: 'en-US',
  messages: {
    a6AoNm: [ { type: 0, value: 'bla' } ],
  },
  defaultLocale: 'en-US'
}

const intl = createIntl(msgObj, cache);
intl.formatMessage({defaultMessage: "bla"});

my stack trace

Error: [@formatjs/intl] An `id` must be provided to format a message.
    at Object.invariant (/Users/user/Projects/project/node_modules/@formatjs/ecma402-abstract/utils.js:87:15)
    at formatMessage (/Users/user/Projects/project/node_modules/@formatjs/intl/src/message.js:34:24)
    at Object.<anonymous> (/Users/user/Projects/project/src/bla/bla.js:8:6)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/user/Projects/project/scripts/bla/bla.js:16:22)

Any ideas how to fix this?

@longlho
Copy link
Member

longlho commented Mar 30, 2021

Did you add our babel-plugin-formatjs?

@TimDaub
Copy link

TimDaub commented Mar 30, 2021

Did you add our babel-plugin-formatjs?

No. Is this how I'd do it?

@longlho
Copy link
Member

longlho commented Mar 30, 2021

In that case you can use https://formatjs.github.io//docs/tooling/linter enforce-id option and it'll autofix it. Otherwise you can transpile your code with either typescript or babel.

@TimDaub
Copy link

TimDaub commented Mar 30, 2021

Thanks for the tip. I got it working with this minimal .eslintrc.js:

$ node --version
v14.16.0
module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 9 // depends on your version of nodejs
  },
  plugins: ["formatjs"],
  rules: {
    "formatjs/enforce-id": [
      "error",
      {
        idInterpolationPattern: "[sha512:contenthash:base64:6]"
      }
    ]
  }
};
$ eslint \"src/**\" -c ./.eslintrc.js --fix

Now regarding this issue. I think many users of formatjs arrive at this issue from Google. I can only speak for myself, but I wish formatjs would have reminded me about configuring babel or eslint. However, from my stack trace, I rather concluded that something must be wrong with e.g. my messages file. Could format js thrown an error to inform the user to install e.g. an id enforcer like babel or eslint when it's clear that e.g. the messages file is not the issue?

@longlho
Copy link
Member

longlho commented Mar 30, 2021

yup good idea. I've clarified the err message

@AndrewEastwood
Copy link

does it mead that every time during development I have to run eslint --fix ?

@longlho
Copy link
Member

longlho commented Dec 15, 2022

U can setup ur ide to fix on save

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

No branches or pull requests