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
Prevent "A message must be provided as a String or AST" error from being throw #1299
Comments
Curious if you can find out what you're passing into it. Maybe we could handle it better. My guess is maybe undefined/null?
This is coming from the intl-messageformat compiler. We're passing something right through to it that isn't a string or AST object in this case. We can very likely catch errors like this and add more details to the thrown error (translation key, params, etc.).
We can add more context but if you want to see where it's coming from I recommend |
I finally tracked it down. It was As I was trying to explain before, typically a single problem like this one isn't hard to fix once identified, but I had to do a lot of digging around to isolate the culprit, so if there is some way I can debug this with DevTools or something I would really like to know. (Maybe it's just me, but most of the time when I look at the stack traces and see glimmer-vm or backburner code I just sigh as I rarely can figure out where my code fits in, even after following the instructions in the infamous "Intrepid developer" comment) |
I've been putting a breakpoint on my In a super simple case like this one: // templates/application.hbs
<h1>Hello, world!</h1>
{{t this.somethingNotDefined}}
<h1>Goodbye, world!</h1> the partially rendered DOM gives a good clue that it happened after "Hello" and before "Goodbye", but in more complex cases (many components, lots of dynamic data, etc.) I haven't found a good way. |
I can add additional context like "called from t helper" with the key included but if you're passing in undefined as the key I can't really pinpoint in what template that originated. The helper API does not expose those kinds of details to us. When passing in undefined as the key I think an error is appropriate since anything less than that would likely mask the bug and make it harder to detect. If you find yourself in this situation often, you could reimplement the t helper to deal with this if a thrown error isn't what you want anymore. |
Looks like
|
95%+ of my text translation is going through
Yeah, I figured, but it seemed worth asking. The "more context" that I was hoping for was, "Which helper?" or "Which component?" or something like that. I suppose I could do something crazy like make my own helper that calls
I respectfully disagree as the bug may still be masked (e.g. value isn't |
This is something you can do in userland by wrapping the |
I've made slight adjustments to include validating key types: And now it should fail validating the type on the key and/or not finding the translation key. I personally prefer to have prod and dev behavior as similar as possible, so I think the idea of not throwing in production builds in this case isn't the best option. Users may not report any issues because things all appear to be fine in the app. Also, it can introduce other side effects in your app only visible in production builds. One example, if you're combining the t helper with another helper/component that might be expecting a string returned. i.e., Now form label is throwing an error and not the t helper
{{form-label label=(t @model.category)}} |
I'm going to close this for now and say #1302 will addresses this by making the failed translation key passed in more obvious than failing trying to parse what isn't an AST object. |
@jacobq @jasonmit We have this issue too! Translation errors can easily slip in and when throwing exceptions Ember blows up and the page won't render. For example the error here, or For us it would be ideal if we could control the outcome, for example via a hook for the common types of errors that can occur. We could then choose whether we'd like to throw an error (probably in dev) or render a fallback message, "Could not translate "my.translation.key" and then send an XHR to Bugsnag/sentry behind the scenes. |
Yes, but they don't have to. The logger could report it back without the user having to do anything (e.g. with TrackJS).
This would not be a problem if
OK, this seems like the best solution for me then. (The keys are very often dynamic.) Thanks for the suggestion. |
5.1.0 will now throw with a more helpful error indicating you've passed in a value that isn't a String as a translation key. This should avoid some instances where you reach the compiler error: |
I still occasionally run into the error message "A message must be provided as a String or AST" with v5.x (addressed by #621?). I realize this is probably my fault (i.e. bug in my app passes bad parameter to
t
helper or something). However, the app would "still work" (aside from missing some text) if the error did not occur, but the error makes Ember blow up, so I would like to downgrade it to a warning. Is this possible?Also, are there practical ways to obtain more information about the context of the error? (e.g. what unacceptable value was received, which tool is throwing it [template helper, service API, etc.], etc.)
Historically, when I've hit these before the solution has been trivial (e.g. adding a default like
(or foo translationKeyForUnknownValues)
), but locating the cause can be a nightmare. UsingerrorOnMissingTranslations: true
doesn't seem to catch dynamic cases (e.g. a "status" value coming from a service).Environment
Steps to Reproduce
The text was updated successfully, but these errors were encountered: