Skip to content

feat: add error fallback option in Encoding.convert#44

Merged
polygonplanet merged 3 commits intomasterfrom
feature/add-fallback-error-option
Jun 8, 2024
Merged

feat: add error fallback option in Encoding.convert#44
polygonplanet merged 3 commits intomasterfrom
feature/add-fallback-error-option

Conversation

@polygonplanet
Copy link
Copy Markdown
Owner

Resolve #43

Added a new { fallback: 'error' } option to Encoding.convert.
This option throws an error if a character cannot be converted, allowing for explicit error handling.

Example:

const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜');
try {
  const sjisArray = Encoding.convert(unicodeArray, {
    to: 'SJIS',
    from: 'UNICODE',
    fallback: 'error' // Specify 'error' to throw an exception
  });
} catch (e) {
  console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153]
}

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

Successfully merging this pull request may close these issues.

Suggestion: Add an "error" fallback option to Encoding.convert to fail loudly if unsupported characters are encountered

1 participant