Skip to main content

Function: createAppSyncI18nMiddleware()

createAppSyncI18nMiddleware<TContext>(__namedParameters): IMiddlewareFunction<unknown, TContext, unknown>

Defined in: i18n.ts:70

Renders a thrown or returned LocalizedError in the request locale before it leaves the Lambda.

Only error.name (AppSync's errorType) and error.message survive the Direct Lambda boundary, so the error is rewritten in place: message becomes the rendered text and name becomes errorType(...). The default is the bare code; see the example to keep the class visible, or pass ({ name }) => name to leave errorType as it was. The error object itself is kept, so markers such as an expected flag still reach whatever reports it. Any other error passes through untouched.

Put it first in middlewares, so it also sees errors thrown by the middlewares after it (authorization gates).

If the catalog cannot be loaded, the error is rethrown unrendered. The client then sees the source-language message rather than losing the error.

Type Parameters​

Type ParameterDefault type
TContextunknown

Parameters​

ParameterTypeDescription
__namedParameters{ catalog: Pick<Catalog, "getI18n">; errorType?: (args) => string; getLocale?: (args) => Requested | Promise<Requested>; }-
__namedParameters.catalogPick<Catalog, "getI18n">-
__namedParameters.errorType?(args) => string-
__namedParameters.getLocale?(args) => Requested | Promise<Requested>The requested locale — a stored user preference, or a list tried in order. Defaults to the request's Accept-Language.

Returns​

IMiddlewareFunction<unknown, TContext, unknown>

Example​

createAppSyncI18nMiddleware({
catalog,
errorType: ({ name, code }) => `${name}[${code}]`,
});