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

Allow jsx values in argument notation when using format-icu #3251

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/format-icu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"src/**/*"
],
"devDependencies": {
"@formatjs/ecma402-abstract": "^1.17.2",
"@formatjs/fast-memoize": "^2.2.0",
"@formatjs/icu-messageformat-parser": "^2.6.2",
"@rollup/plugin-node-resolve": "13.3.0",
"@rollup/plugin-typescript": "8.3.4",
"@testing-library/jest-dom": "^5.16.5",
Expand All @@ -57,7 +60,6 @@
"@types/node": "^17.0.8",
"@types/testing-library__jest-dom": "^5.14.5",
"concurrently": "7.3.0",
"intl-messageformat": "^9.9.1",
"jest": "^27.2.4",
"jest-fetch-mock": "^3.0.3",
"rollup": "^2.56.3",
Expand Down
65 changes: 65 additions & 0 deletions packages/format-icu/src/IntlMessageFormat/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export enum ErrorCode {
// When we have a placeholder but no value to format
MISSING_VALUE = 'MISSING_VALUE',
// When value supplied is invalid
INVALID_VALUE = 'INVALID_VALUE',
// When we need specific Intl API but it's not available
MISSING_INTL_API = 'MISSING_INTL_API',
}

export class FormatError extends Error {
public readonly code: ErrorCode;
/**
* Original message we're trying to format
* `undefined` if we're only dealing w/ AST
*
* @type {(string | undefined)}
* @memberof FormatError
*/
public readonly originalMessage: string | undefined;
constructor(msg: string, code: ErrorCode, originalMessage?: string) {
super(msg);
this.code = code;
this.originalMessage = originalMessage;
}
public toString() {
return `[formatjs Error: ${this.code}] ${this.message}`;
}
}

export class InvalidValueError extends FormatError {
constructor(
variableId: string,
value: any,
options: string[],
originalMessage?: string
) {
super(
`Invalid values for "${variableId}": "${value}". Options are "${Object.keys(
options
).join('", "')}"`,
ErrorCode.INVALID_VALUE,
originalMessage
);
}
}

export class InvalidValueTypeError extends FormatError {
constructor(value: any, type: string, originalMessage?: string) {
super(
`Value for "${value}" must be of type ${type}`,
ErrorCode.INVALID_VALUE,
originalMessage
);
}
}

export class MissingValueError extends FormatError {
constructor(variableId: string, originalMessage?: string) {
super(
`The intl string context variable "${variableId}" was not provided to the string "${originalMessage}"`,
ErrorCode.MISSING_VALUE,
originalMessage
);
}
}
Loading