From 7aaba3edb0b71c49ecaa694146e6d7342c884f26 Mon Sep 17 00:00:00 2001 From: Stanislav Mayorov Date: Wed, 4 Nov 2026 16:52:58 +0300 Subject: [PATCH 1/2] fix: map native error codes missing from ErrorCodeMapping iOS 4.3.2 codes 2, 1020, 1030, 1050, 1060 (billingPlanUnavailable), 3020 and Android 4.3.x codes 3020, 4104-4107 reached JS with no name: getErrorCode did not know them and comparing adaptyCode with them was a type error. --- src/types/error.test.ts | 32 ++++++++++++++++++++++++++++++++ src/types/error.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/types/error.test.ts diff --git a/src/types/error.test.ts b/src/types/error.test.ts new file mode 100644 index 0000000..c981b8a --- /dev/null +++ b/src/types/error.test.ts @@ -0,0 +1,32 @@ +import { + ErrorCode, + ErrorCodeName, + getErrorCode, + getErrorPrompt, +} from './error'; + +describe('error codes', () => { + it('keeps ErrorCode and ErrorCodeName in sync', () => { + const fromMapping = Object.entries(ErrorCode).map( + ([code, name]) => [name, Number(code)] as const, + ); + + expect(Object.fromEntries(fromMapping)).toStrictEqual(ErrorCodeName); + }); + + it.each([ + [2, 'paymentCancelled'], + [1020, 'fetchSubscriptionStatusFailed'], + [1030, 'unknownTransactionId'], + [1050, 'paymentPendingError'], + [1060, 'billingPlanUnavailable'], + [3020, 'unidentifiedUserLogout'], + [4104, 'wrongAssetType'], + [4105, 'jsException'], + [4106, 'navigatorNotFound'], + [4107, 'invalidActionUrl'], + ] as const)('maps native code %i to %s', (code, name) => { + expect(getErrorPrompt(code)).toBe(name); + expect(getErrorCode(name)).toBe(code); + }); +}); diff --git a/src/types/error.ts b/src/types/error.ts index 28e91b7..943fffd 100644 --- a/src/types/error.ts +++ b/src/types/error.ts @@ -10,6 +10,10 @@ const ErrorCodeMapping = Object.freeze({ * Client is not allowed to make a request, etc. */ 1: 'clientInvalid', + /** + * The user cancelled the payment request. + */ + 2: 'paymentCancelled', /** * Invalid purchase identifier, etc. */ @@ -95,6 +99,17 @@ const ErrorCodeMapping = Object.freeze({ 1006: 'productPurchaseFailed', 1010: 'refreshReceiptFailed', 1011: 'receiveRestoredTransactionsFailed', + 1020: 'fetchSubscriptionStatusFailed', + 1030: 'unknownTransactionId', + /** + * The payment is deferred, e.g. awaiting Ask to Buy approval. + */ + 1050: 'paymentPendingError', + /** + * The product doesn't offer the requested billing plan on this device + * (e.g. a 12-month commitment plan below iOS 26.4). iOS only. + */ + 1060: 'billingPlanUnavailable', /** * You need to be authenticated to perform requests. @@ -120,6 +135,10 @@ const ErrorCodeMapping = Object.freeze({ */ 3006: 'profileWasChanged', 3007: 'unsupportedData', + /** + * `logout` was called for a user that was never identified. + */ + 3020: 'unidentifiedUserLogout', 3100: 'persistingDataError', 3101: 'fetchTimeoutError', /** @@ -132,6 +151,13 @@ const ErrorCodeMapping = Object.freeze({ * (e.g. no foreground-active window scene to show store messages). */ 3202: 'resolverFailure', + /** + * Custom Android AdaptyUI codes. + */ + 4104: 'wrongAssetType', + 4105: 'jsException', + 4106: 'navigatorNotFound', + 4107: 'invalidActionUrl', 9000: 'operationInterrupted', }); @@ -141,6 +167,7 @@ export type ErrorCode = keyof typeof ErrorCodeMapping; export const ErrorCodeName = Object.freeze({ unknown: 0, clientInvalid: 1, + paymentCancelled: 2, paymentInvalid: 3, paymentNotAllowed: 4, storeProductNotAvailable: 5, @@ -174,6 +201,10 @@ export const ErrorCodeName = Object.freeze({ productPurchaseFailed: 1006, refreshReceiptFailed: 1010, receiveRestoredTransactionsFailed: 1011, + fetchSubscriptionStatusFailed: 1020, + unknownTransactionId: 1030, + paymentPendingError: 1050, + billingPlanUnavailable: 1060, notActivated: 2002, badRequest: 2003, serverError: 2004, @@ -185,10 +216,15 @@ export const ErrorCodeName = Object.freeze({ activateOnceError: 3005, profileWasChanged: 3006, unsupportedData: 3007, + unidentifiedUserLogout: 3020, persistingDataError: 3100, fetchTimeoutError: 3101, operationInProgress: 3201, resolverFailure: 3202, + wrongAssetType: 4104, + jsException: 4105, + navigatorNotFound: 4106, + invalidActionUrl: 4107, operationInterrupted: 9000, } as const); export type ErrorCodeName = keyof typeof ErrorCodeName; From dd2acc7b68a24e48a8d027eae54a4606df43bb5b Mon Sep 17 00:00:00 2001 From: Stanislav Mayorov Date: Wed, 4 Nov 2026 19:40:11 +0300 Subject: [PATCH 2/2] feat: accept error codes unknown to this SDK version ErrorCode becomes KnownErrorCode | (number & {}): known codes still autocomplete, and comparing adaptyCode with a code that a newer native SDK sends is no longer a type error. Add isKnownErrorCode, getErrorCodeName and AdaptyError.codeName; an unknown code is shown as "(unknown code)" instead of "(undefined)" in the error message, and getErrorPrompt no longer casts. --- src/adapty-error.ts | 9 +++++-- src/coders/adapty-native-error.ts | 2 +- src/types/error.test.ts | 25 +++++++++++++++++++ src/types/error.ts | 40 ++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/adapty-error.ts b/src/adapty-error.ts index 2588cbd..f3c093b 100644 --- a/src/adapty-error.ts +++ b/src/adapty-error.ts @@ -1,4 +1,4 @@ -import { ErrorCode } from '@/types/error'; +import { ErrorCode, ErrorCodeName, getErrorCodeName } from '@/types/error'; export interface AdaptyErrorInput { adaptyCode: ErrorCode; @@ -29,6 +29,11 @@ export class AdaptyError extends Error { } } + /** Name of {@link adaptyCode}, or `undefined` if the code is newer than this SDK version. */ + public get codeName(): ErrorCodeName | undefined { + return getErrorCodeName(this.adaptyCode); + } + public static set onError(callback: (error: AdaptyError) => void) { AdaptyError.middleware = callback; } @@ -60,7 +65,7 @@ export class AdaptyError extends Error { private static getMessage(input: AdaptyErrorInput): string { const code = input.adaptyCode; - const codeText = ErrorCode[code]; + const codeText = getErrorCodeName(code) ?? 'unknown code'; let message = `#${code} (${codeText}): ${input.message}`; diff --git a/src/coders/adapty-native-error.ts b/src/coders/adapty-native-error.ts index 7848aa9..c86c5b8 100644 --- a/src/coders/adapty-native-error.ts +++ b/src/coders/adapty-native-error.ts @@ -22,7 +22,7 @@ export class AdaptyNativeErrorCoder public getError(data: Model): AdaptyError { return new AdaptyError({ - adaptyCode: data.adaptyCode as AdaptyError['adaptyCode'], + adaptyCode: data.adaptyCode, message: data.message, detail: data.detail, }); diff --git a/src/types/error.test.ts b/src/types/error.test.ts index c981b8a..1ab7b45 100644 --- a/src/types/error.test.ts +++ b/src/types/error.test.ts @@ -1,8 +1,11 @@ +import { AdaptyError } from '@/adapty-error'; import { ErrorCode, ErrorCodeName, getErrorCode, + getErrorCodeName, getErrorPrompt, + isKnownErrorCode, } from './error'; describe('error codes', () => { @@ -29,4 +32,26 @@ describe('error codes', () => { expect(getErrorPrompt(code)).toBe(name); expect(getErrorCode(name)).toBe(code); }); + + it('accepts a code unknown to this SDK version', () => { + const error = new AdaptyError({ adaptyCode: 7777, message: 'New error' }); + + expect(isKnownErrorCode(7777)).toBe(false); + expect(getErrorCodeName(7777)).toBeUndefined(); + expect(getErrorPrompt(7777)).toBe('Unknown code: 7777'); + expect(error.adaptyCode === 7777).toBe(true); + expect(error.codeName).toBeUndefined(); + expect(error.message).toBe('#7777 (unknown code): New error'); + }); + + it('names a known code', () => { + const error = new AdaptyError({ adaptyCode: 1060, message: 'Unavailable' }); + + expect(isKnownErrorCode(1060)).toBe(true); + expect(error.adaptyCode === ErrorCodeName.billingPlanUnavailable).toBe( + true, + ); + expect(error.codeName).toBe('billingPlanUnavailable'); + expect(error.message).toBe('#1060 (billingPlanUnavailable): Unavailable'); + }); }); diff --git a/src/types/error.ts b/src/types/error.ts index 943fffd..d6e88ee 100644 --- a/src/types/error.ts +++ b/src/types/error.ts @@ -162,7 +162,16 @@ const ErrorCodeMapping = Object.freeze({ }); export const ErrorCode = ErrorCodeMapping; -export type ErrorCode = keyof typeof ErrorCodeMapping; +/** A code known to this version of the SDK. */ +export type KnownErrorCode = keyof typeof ErrorCodeMapping; +/** + * Code of an {@link AdaptyError}. + * + * @remarks + * Native SDKs may send codes this version doesn't know yet, so any number is accepted. + * Known codes still autocomplete; use {@link isKnownErrorCode} to narrow. + */ +export type ErrorCode = KnownErrorCode | (number & {}); export const ErrorCodeName = Object.freeze({ unknown: 0, @@ -229,11 +238,18 @@ export const ErrorCodeName = Object.freeze({ } as const); export type ErrorCodeName = keyof typeof ErrorCodeName; -export function getErrorCode( - error: (typeof ErrorCode)[ErrorCode], -): ErrorCode | undefined { +export function isKnownErrorCode(code: number): code is KnownErrorCode { + return Object.prototype.hasOwnProperty.call(ErrorCodeMapping, code); +} + +/** Returns the name of a code, or `undefined` for a code unknown to this SDK version. */ +export function getErrorCodeName(code: number): ErrorCodeName | undefined { + return isKnownErrorCode(code) ? ErrorCodeMapping[code] : undefined; +} + +export function getErrorCode(error: ErrorCodeName): KnownErrorCode | undefined { const errorCode = Object.keys(ErrorCode).find(keyStr => { - const key = Number(keyStr) as ErrorCode; + const key = Number(keyStr) as KnownErrorCode; return ErrorCode[key] === error; }); @@ -241,15 +257,11 @@ export function getErrorCode( return undefined; } - return Number(errorCode) as ErrorCode; + return Number(errorCode) as KnownErrorCode; } -export function getErrorPrompt(code: ErrorCode): (typeof ErrorCode)[ErrorCode] { - const prompt = ErrorCode[code]; - - if (!prompt) { - return `Unknown code: ${code}` as (typeof ErrorCode)[ErrorCode]; - } - - return prompt; +export function getErrorPrompt( + code: ErrorCode, +): ErrorCodeName | `Unknown code: ${number}` { + return getErrorCodeName(code) ?? `Unknown code: ${code}`; }