// Copyright © 2022 Olo Inc. All rights reserved. // This software is made available under the Olo Pay SDK License (See LICENSE.md file) package com.olopaysdkreactnative.extensions import com.facebook.react.bridge.Promise import com.olo.olopay.data.CardErrorType import com.olo.olopay.exceptions.* import com.olopaysdkreactnative.data.ErrorCodes fun Promise.rejectException(e: OloPayException) { val errorCode = getErrorCode(e) reject(errorCode, getErrorMessage(e, "Unexpected error occurred")) } private fun getErrorMessage(e: OloPayException, defaultMessage: String): String { return if (e.message.isNullOrEmpty()) { defaultMessage } else { e.message!! } } private fun getErrorCode(e: OloPayException): String { val cardException = e as? CardException if (cardException != null) { return getErrorCode(cardException.type) } if (e is ApiException) return ErrorCodes.ApiError if (e is InvalidRequestException) return ErrorCodes.InvalidRequest if (e is ApiConnectionException) return ErrorCodes.Connection if (e is RateLimitException) return ErrorCodes.RateLimit return ErrorCodes.GeneralError } private fun getErrorCode(cardErrorType: CardErrorType): String { return when (cardErrorType) { CardErrorType.InvalidNumber -> ErrorCodes.InvalidNumber CardErrorType.InvalidExpMonth -> ErrorCodes.InvalidExpiration CardErrorType.InvalidExpYear -> ErrorCodes.InvalidExpiration CardErrorType.InvalidCVV -> ErrorCodes.InvalidCvv CardErrorType.InvalidZip -> ErrorCodes.InvalidPostalCode CardErrorType.ExpiredCard -> ErrorCodes.ExpiredCard CardErrorType.CardDeclined -> ErrorCodes.CardDeclined CardErrorType.ProcessingError -> ErrorCodes.ProcessingError CardErrorType.UnknownCardError -> ErrorCodes.UnknownCardError } }