Skip to content

Commit

Permalink
feat(@formatjs/intl): upgrade to TS 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Oct 18, 2021
1 parent 8b9b381 commit 8037bb2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"tslib": "^2.1.0"
},
"peerDependencies": {
"typescript": "^4.3"
"typescript": "^4.4"
},
"peerDependenciesMeta": {
"typescript": {
Expand Down
35 changes: 26 additions & 9 deletions packages/intl/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ export class IntlError<
> extends Error {
public readonly code: T

constructor(code: T, message: string, exception?: Error) {
constructor(code: T, message: string, exception?: Error | unknown) {
const err = exception
? exception instanceof Error
? exception
: new Error(String(exception))
: undefined
super(
`[@formatjs/intl Error ${code}] ${message}
${exception ? `\n${exception.message}\n${exception.stack}` : ''}`
${err ? `\n${err.message}\n${err.stack}` : ''}`
)
this.code = code
// @ts-ignore just so we don't need to declare dep on @types/node
Expand All @@ -28,39 +33,51 @@ ${exception ? `\n${exception.message}\n${exception.stack}` : ''}`
}

export class UnsupportedFormatterError extends IntlError<IntlErrorCode.UNSUPPORTED_FORMATTER> {
constructor(message: string, exception?: Error) {
constructor(message: string, exception?: Error | unknown) {
super(IntlErrorCode.UNSUPPORTED_FORMATTER, message, exception)
}
}

export class InvalidConfigError extends IntlError<IntlErrorCode.INVALID_CONFIG> {
constructor(message: string, exception?: Error) {
constructor(message: string, exception?: Error | unknown) {
super(IntlErrorCode.INVALID_CONFIG, message, exception)
}
}

export class MissingDataError extends IntlError<IntlErrorCode.MISSING_DATA> {
constructor(message: string, exception?: Error) {
constructor(message: string, exception?: Error | unknown) {
super(IntlErrorCode.MISSING_DATA, message, exception)
}
}

export class MessageFormatError extends IntlError<IntlErrorCode.FORMAT_ERROR> {
export class IntlFormatError extends IntlError<IntlErrorCode.FORMAT_ERROR> {
public readonly descriptor?: MessageDescriptor
constructor(message: string, locale: string, exception?: Error | unknown) {
super(
IntlErrorCode.FORMAT_ERROR,
`${message}
Locale: ${locale}
`,
exception
)
}
}

export class MessageFormatError extends IntlFormatError {
public readonly descriptor?: MessageDescriptor
constructor(
message: string,
locale: string,
descriptor?: MessageDescriptor,
exception?: Error
exception?: Error | unknown
) {
super(
IntlErrorCode.FORMAT_ERROR,
`${message}
Locale: ${locale}
MessageID: ${descriptor?.id}
Default Message: ${descriptor?.defaultMessage}
Description: ${descriptor?.description}
`,
locale,
exception
)
this.descriptor = descriptor
Expand Down
4 changes: 2 additions & 2 deletions packages/intl/src/plural.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Formatters, IntlFormatters, OnErrorFn} from './types'
import {filterProps} from './utils'
import {MessageFormatError} from './error'
import {IntlFormatError} from './error'
import {ErrorCode, FormatError} from 'intl-messageformat'
import {LDMLPluralRule} from '@formatjs/ecma402-abstract'

Expand Down Expand Up @@ -38,7 +38,7 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
value
) as LDMLPluralRule
} catch (e) {
onError(new MessageFormatError('Error formatting plural.', e))
onError(new IntlFormatError('Error formatting plural.', locale, e))
}

return 'other'
Expand Down
6 changes: 4 additions & 2 deletions packages/intl/src/relativeTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {IntlFormatters, Formatters, CustomFormats, OnErrorFn} from './types'

import {getNamedFormat, filterProps} from './utils'
import {FormatError, ErrorCode} from 'intl-messageformat'
import {MessageFormatError} from './error'
import {IntlFormatError} from './error'

const RELATIVE_TIME_FORMAT_OPTIONS: Array<
keyof Intl.RelativeTimeFormatOptions
Expand Down Expand Up @@ -65,7 +65,9 @@ Try polyfilling it using "@formatjs/intl-relativetimeformat"
unit
)
} catch (e) {
config.onError(new MessageFormatError('Error formatting relative time.', e))
config.onError(
new IntlFormatError('Error formatting relative time.', config.locale, e)
)
}

return String(value)
Expand Down

0 comments on commit 8037bb2

Please sign in to comment.