diff --git a/core/api/dev/apollo-federation/supergraph.graphql b/core/api/dev/apollo-federation/supergraph.graphql index cae3ab17b3b..fdd2640df93 100644 --- a/core/api/dev/apollo-federation/supergraph.graphql +++ b/core/api/dev/apollo-federation/supergraph.graphql @@ -1372,6 +1372,7 @@ type QuizCompletedPayload { errors: [Error!]! quiz: Quiz + rewardPaid: Boolean! } type QuizQuestion diff --git a/core/api/src/app/index.types.d.ts b/core/api/src/app/index.types.d.ts index 41146e4f33f..8341f1be431 100644 --- a/core/api/src/app/index.types.d.ts +++ b/core/api/src/app/index.types.d.ts @@ -1,8 +1,22 @@ -type PartialResult = { - result: T | null - error?: ApplicationError - partialResult: true -} +type PartialResultType = + (typeof import("./partial-result").PartialResultType)[keyof typeof import("./partial-result").PartialResultType] + +type PartialResult = + | { + result: T + error?: undefined + type: typeof import("./partial-result").PartialResultType.Ok + } + | { + result: T + error: ApplicationError + type: typeof import("./partial-result").PartialResultType.Partial + } + | { + result: null + error: ApplicationError + type: typeof import("./partial-result").PartialResultType.Err + } type ValueOf = T[keyof T] diff --git a/core/api/src/app/partial-result.ts b/core/api/src/app/partial-result.ts index 9c10a546adb..f835f5708d4 100644 --- a/core/api/src/app/partial-result.ts +++ b/core/api/src/app/partial-result.ts @@ -1,16 +1,22 @@ export const PartialResult = { ok: (result: T): PartialResult => ({ result, - partialResult: true, + type: PartialResultType.Ok, }), partial: (result: T, error: ApplicationError): PartialResult => ({ result, error, - partialResult: true, + type: PartialResultType.Partial, }), err: (error: ApplicationError): PartialResult => ({ result: null, error, - partialResult: true, + type: PartialResultType.Err, }), } + +export const PartialResultType = { + Partial: "Partial", + Ok: "Ok", + Err: "Err", +} as const diff --git a/core/api/src/app/payments/add-earn.ts b/core/api/src/app/payments/add-earn.ts index 3a0ae50396f..406da887a69 100644 --- a/core/api/src/app/payments/add-earn.ts +++ b/core/api/src/app/payments/add-earn.ts @@ -1,3 +1,5 @@ +import { PartialResult } from "../partial-result" + import { intraledgerPaymentSendWalletIdForBtcWallet } from "./send-intraledger" import { getRewardsConfig, OnboardingEarn } from "@/config" @@ -30,14 +32,13 @@ export const addEarn = async ({ quizQuestionId: string accountId: string }): Promise< - | { - quizQuestion: Quiz - rewardPaymentError?: ApplicationError - } - | ApplicationError + PartialResult<{ + quiz: Quiz + rewardPaid: boolean + }> > => { const accountId = checkedToAccountId(accountIdRaw) - if (accountId instanceof Error) return accountId + if (accountId instanceof Error) return PartialResult.err(accountId) const rewardsConfig = getRewardsConfig() @@ -45,25 +46,26 @@ export const addEarn = async ({ const quizQuestionId = quizQuestionIdString as QuizQuestionId const amount = OnboardingEarn[quizQuestionId] - if (!amount) return new InvalidQuizQuestionIdError() + if (!amount) return PartialResult.err(new InvalidQuizQuestionIdError()) const funderWalletId = await getFunderWalletId() const funderWallet = await WalletsRepository().findById(funderWalletId) - if (funderWallet instanceof Error) return funderWallet + if (funderWallet instanceof Error) return PartialResult.err(funderWallet) const funderAccount = await AccountsRepository().findById(funderWallet.accountId) - if (funderAccount instanceof Error) return funderAccount + if (funderAccount instanceof Error) return PartialResult.err(funderAccount) const recipientAccount = await AccountsRepository().findById(accountId) - if (recipientAccount instanceof Error) return recipientAccount + if (recipientAccount instanceof Error) return PartialResult.err(recipientAccount) const user = await UsersRepository().findById(recipientAccount.kratosUserId) - if (user instanceof Error) return user + if (user instanceof Error) return PartialResult.err(user) const isFirstTimeAnsweringQuestion = await RewardsRepository(accountId).add(quizQuestionId) - if (isFirstTimeAnsweringQuestion instanceof Error) return isFirstTimeAnsweringQuestion + if (isFirstTimeAnsweringQuestion instanceof Error) + return PartialResult.err(isFirstTimeAnsweringQuestion) - const quizQuestion: Quiz = { + const quiz: Quiz = { id: quizQuestionId, amount: amount, completed: true, @@ -74,14 +76,24 @@ export const addEarn = async ({ ).authorize(user.phoneMetadata) if (validatedPhoneMetadata instanceof Error) { - return { - quizQuestion, - rewardPaymentError: new InvalidPhoneForRewardError(validatedPhoneMetadata.name), - } + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + new InvalidPhoneForRewardError(validatedPhoneMetadata.name), + ) } const accountIP = await AccountsIpsRepository().findLastByAccountId(recipientAccount.id) - if (accountIP instanceof Error) return accountIP + if (accountIP instanceof Error) + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + new InvalidPhoneForRewardError(accountIP), + ) const validatedIPMetadata = IPMetadataAuthorizer( rewardsConfig.ipMetadataValidationSettings, @@ -89,38 +101,53 @@ export const addEarn = async ({ if (validatedIPMetadata instanceof Error) { if (validatedIPMetadata instanceof MissingIPMetadataError) - return { - quizQuestion, - rewardPaymentError: new InvalidIpMetadataError(validatedIPMetadata), - } + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + new InvalidIpMetadataError(validatedIPMetadata), + ) if (validatedIPMetadata instanceof UnauthorizedIPError) - return { - quizQuestion, - rewardPaymentError: validatedIPMetadata, - } - - return { - quizQuestion, - rewardPaymentError: new UnknownRepositoryError("add earn error"), - } + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + validatedIPMetadata, + ) + + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + new UnknownRepositoryError("add earn error"), + ) } const recipientWallets = await WalletsRepository().listByAccountId(accountId) if (recipientWallets instanceof Error) - return { - quizQuestion, - rewardPaymentError: recipientWallets, - } + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + recipientWallets, + ) const recipientBtcWallet = recipientWallets.find( (wallet) => wallet.currency === WalletCurrency.Btc, ) if (recipientBtcWallet === undefined) - return { - quizQuestion, - rewardPaymentError: new NoBtcWalletExistsForAccountError(), - } + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + new NoBtcWalletExistsForAccountError(), + ) const recipientWalletId = recipientBtcWallet.id @@ -132,10 +159,19 @@ export const addEarn = async ({ senderAccount: funderAccount, }) - return { - quizQuestion, - rewardPaymentError: payment instanceof Error ? payment : undefined, - } + if (payment instanceof Error) + return PartialResult.partial( + { + quiz, + rewardPaid: false, + }, + payment, + ) + + return PartialResult.ok({ + quiz, + rewardPaid: true, + }) } export const isAccountEligibleForEarnPayment = async ({ diff --git a/core/api/src/domain/bitcoin/lightning/errors.ts b/core/api/src/domain/bitcoin/lightning/errors.ts index c9bd4b45cb0..5ba3b0bbb52 100644 --- a/core/api/src/domain/bitcoin/lightning/errors.ts +++ b/core/api/src/domain/bitcoin/lightning/errors.ts @@ -50,9 +50,6 @@ export class DestinationMissingDependentFeatureError extends LightningServiceErr export class LookupPaymentTimedOutError extends LightningServiceError { level = ErrorLevel.Critical } -export class InvalidFeeProbeStateError extends LightningServiceError { - level = ErrorLevel.Critical -} export class UnknownRouteNotFoundError extends LightningServiceError { level = ErrorLevel.Critical diff --git a/core/api/src/domain/errors.ts b/core/api/src/domain/errors.ts index 1fd4d8006f3..f2cbb288a33 100644 --- a/core/api/src/domain/errors.ts +++ b/core/api/src/domain/errors.ts @@ -65,7 +65,6 @@ export class CouldNotFindLnPaymentFromHashError extends CouldNotFindError { export class CouldNotFindAccountFromIdError extends CouldNotFindError {} export class CouldNotFindAccountFromUsernameError extends CouldNotFindError {} export class CouldNotFindAccountFromPhoneError extends CouldNotFindError {} -export class CouldNotFindTransactionsForAccountError extends CouldNotFindError {} export class CouldNotFindAccountFromKratosIdError extends CouldNotFindError {} export class RewardAlreadyPresentError extends DomainError {} diff --git a/core/api/src/graphql/error-map.ts b/core/api/src/graphql/error-map.ts index d99ae1d4c01..93ff3cdd410 100644 --- a/core/api/src/graphql/error-map.ts +++ b/core/api/src/graphql/error-map.ts @@ -96,10 +96,6 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => { message = `The associated lightning invoice could not be found.` return new NotFoundError({ message, logger: baseLogger }) - case "CouldNotFindTransactionsForAccountError": - message = "No transactions found for your account." - return new NotFoundError({ message, logger: baseLogger }) - case "CouldNotFindUserFromPhoneError": message = `User does not exist for phone ${error.message}` return new NotFoundError({ message, logger: baseLogger }) @@ -591,7 +587,6 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => { case "BigIntConversionError": case "BigIntFloatConversionError": case "SafeWrapperError": - case "InvalidFeeProbeStateError": case "LookupPaymentTimedOutError": case "InvalidPubKeyError": case "SkipProbeForPubkeyError": diff --git a/core/api/src/graphql/public/root/mutation/ln-invoice-fee-probe.ts b/core/api/src/graphql/public/root/mutation/ln-invoice-fee-probe.ts index 22a3991e979..4b8c9168fba 100644 --- a/core/api/src/graphql/public/root/mutation/ln-invoice-fee-probe.ts +++ b/core/api/src/graphql/public/root/mutation/ln-invoice-fee-probe.ts @@ -1,7 +1,5 @@ import { normalizePaymentAmount } from "../../../shared/root/mutation" -import { InvalidFeeProbeStateError } from "@/domain/bitcoin/lightning" - import { Payments } from "@/app" import { GT } from "@/graphql/index" @@ -9,6 +7,7 @@ import WalletId from "@/graphql/shared/types/scalar/wallet-id" import SatAmountPayload from "@/graphql/public/types/payload/sat-amount" import LnPaymentRequest from "@/graphql/shared/types/scalar/ln-payment-request" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" +import { PartialResultType } from "@/app/partial-result" const LnInvoiceFeeProbeInput = GT.Input({ name: "LnInvoiceFeeProbeInput", @@ -43,31 +42,29 @@ const LnInvoiceFeeProbeMutation = GT.Field< if (paymentRequest instanceof Error) return { errors: [{ message: paymentRequest.message }] } - const { result: feeSatAmount, error } = - await Payments.getLightningFeeEstimationForBtcWallet({ - walletId, - uncheckedPaymentRequest: paymentRequest, - }) + const { + result: feeSatAmount, + error, + type, + } = await Payments.getLightningFeeEstimationForBtcWallet({ + walletId, + uncheckedPaymentRequest: paymentRequest, + }) - if (feeSatAmount !== null && error instanceof Error) { + if (type === PartialResultType.Partial) { + error return { errors: [mapAndParseErrorForGqlResponse(error)], ...normalizePaymentAmount(feeSatAmount), } } - if (error instanceof Error) { + if (type === PartialResultType.Err) { return { errors: [mapAndParseErrorForGqlResponse(error)], } } - if (feeSatAmount === null) { - return { - errors: [mapAndParseErrorForGqlResponse(new InvalidFeeProbeStateError())], - } - } - return { errors: [], ...normalizePaymentAmount(feeSatAmount), diff --git a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-fee-probe.ts b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-fee-probe.ts index 2abbfc867f5..72b5e5f8080 100644 --- a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-fee-probe.ts +++ b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-fee-probe.ts @@ -1,7 +1,5 @@ import { normalizePaymentAmount } from "../../../shared/root/mutation" -import { InvalidFeeProbeStateError } from "@/domain/bitcoin/lightning" - import { Payments } from "@/app" import { GT } from "@/graphql/index" @@ -10,6 +8,7 @@ import SatAmount from "@/graphql/shared/types/scalar/sat-amount" import SatAmountPayload from "@/graphql/public/types/payload/sat-amount" import LnPaymentRequest from "@/graphql/shared/types/scalar/ln-payment-request" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" +import { PartialResultType } from "@/app/partial-result" const LnNoAmountInvoiceFeeProbeInput = GT.Input({ name: "LnNoAmountInvoiceFeeProbeInput", @@ -37,32 +36,29 @@ const LnNoAmountInvoiceFeeProbeMutation = GT.Field({ } } - const { result: feeSatAmount, error } = - await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ - walletId, - amount, - uncheckedPaymentRequest: paymentRequest, - }) + const { + result: feeSatAmount, + error, + type, + } = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ + walletId, + amount, + uncheckedPaymentRequest: paymentRequest, + }) - if (feeSatAmount !== null && error instanceof Error) { + if (type === PartialResultType.Partial) { return { errors: [mapAndParseErrorForGqlResponse(error)], ...normalizePaymentAmount(feeSatAmount), } } - if (error instanceof Error) { + if (type === PartialResultType.Err) { return { errors: [mapAndParseErrorForGqlResponse(error)], } } - if (feeSatAmount === null) { - return { - errors: [mapAndParseErrorForGqlResponse(new InvalidFeeProbeStateError())], - } - } - return { errors: [], ...normalizePaymentAmount(feeSatAmount), diff --git a/core/api/src/graphql/public/root/mutation/ln-noamount-usd-invoice-fee-probe.ts b/core/api/src/graphql/public/root/mutation/ln-noamount-usd-invoice-fee-probe.ts index acdb977f871..72c417d345a 100644 --- a/core/api/src/graphql/public/root/mutation/ln-noamount-usd-invoice-fee-probe.ts +++ b/core/api/src/graphql/public/root/mutation/ln-noamount-usd-invoice-fee-probe.ts @@ -1,7 +1,5 @@ import { normalizePaymentAmount } from "../../../shared/root/mutation" -import { InvalidFeeProbeStateError } from "@/domain/bitcoin/lightning" - import { Payments } from "@/app" import { GT } from "@/graphql/index" @@ -10,6 +8,7 @@ import CentAmount from "@/graphql/public/types/scalar/cent-amount" import CentAmountPayload from "@/graphql/public/types/payload/cent-amount" import LnPaymentRequest from "@/graphql/shared/types/scalar/ln-payment-request" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" +import { PartialResultType } from "@/app/partial-result" const LnNoAmountUsdInvoiceFeeProbeInput = GT.Input({ name: "LnNoAmountUsdInvoiceFeeProbeInput", @@ -37,32 +36,29 @@ const LnNoAmountUsdInvoiceFeeProbeMutation = GT.Field({ } } - const { result: feeSatAmount, error } = - await Payments.getNoAmountLightningFeeEstimationForUsdWallet({ - walletId, - amount, - uncheckedPaymentRequest: paymentRequest, - }) + const { + result: feeSatAmount, + error, + type, + } = await Payments.getNoAmountLightningFeeEstimationForUsdWallet({ + walletId, + amount, + uncheckedPaymentRequest: paymentRequest, + }) - if (feeSatAmount !== null && error instanceof Error) { + if (type === PartialResultType.Partial) { return { errors: [mapAndParseErrorForGqlResponse(error)], ...normalizePaymentAmount(feeSatAmount), } } - if (error instanceof Error) { + if (type === PartialResultType.Err) { return { errors: [mapAndParseErrorForGqlResponse(error)], } } - if (feeSatAmount === null) { - return { - errors: [mapAndParseErrorForGqlResponse(new InvalidFeeProbeStateError())], - } - } - return { errors: [], ...normalizePaymentAmount(feeSatAmount), diff --git a/core/api/src/graphql/public/root/mutation/ln-usd-invoice-fee-probe.ts b/core/api/src/graphql/public/root/mutation/ln-usd-invoice-fee-probe.ts index b65b1b2866d..2e29955db2b 100644 --- a/core/api/src/graphql/public/root/mutation/ln-usd-invoice-fee-probe.ts +++ b/core/api/src/graphql/public/root/mutation/ln-usd-invoice-fee-probe.ts @@ -1,7 +1,5 @@ import { normalizePaymentAmount } from "../../../shared/root/mutation" -import { InvalidFeeProbeStateError } from "@/domain/bitcoin/lightning" - import { Payments } from "@/app" import { GT } from "@/graphql/index" @@ -11,6 +9,7 @@ import LnPaymentRequest from "@/graphql/shared/types/scalar/ln-payment-request" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" import { checkedToWalletId } from "@/domain/wallets" +import { PartialResultType } from "@/app/partial-result" const LnUsdInvoiceFeeProbeInput = GT.Input({ name: "LnUsdInvoiceFeeProbeInput", @@ -52,31 +51,28 @@ const LnUsdInvoiceFeeProbeMutation = GT.Field< if (walletIdChecked instanceof Error) return { errors: [mapAndParseErrorForGqlResponse(walletIdChecked)] } - const { result: feeSatAmount, error } = - await Payments.getLightningFeeEstimationForUsdWallet({ - walletId, - uncheckedPaymentRequest: paymentRequest, - }) + const { + result: feeSatAmount, + error, + type, + } = await Payments.getLightningFeeEstimationForUsdWallet({ + walletId, + uncheckedPaymentRequest: paymentRequest, + }) - if (feeSatAmount !== null && error instanceof Error) { + if (type === PartialResultType.Partial) { return { errors: [mapAndParseErrorForGqlResponse(error)], ...normalizePaymentAmount(feeSatAmount), } } - if (error instanceof Error) { + if (type === PartialResultType.Err) { return { errors: [mapAndParseErrorForGqlResponse(error)], } } - if (feeSatAmount === null) { - return { - errors: [mapAndParseErrorForGqlResponse(new InvalidFeeProbeStateError())], - } - } - return { errors: [], ...normalizePaymentAmount(feeSatAmount), diff --git a/core/api/src/graphql/public/root/mutation/quiz-completed.ts b/core/api/src/graphql/public/root/mutation/quiz-completed.ts index 716568453ff..33e96d3eb6b 100644 --- a/core/api/src/graphql/public/root/mutation/quiz-completed.ts +++ b/core/api/src/graphql/public/root/mutation/quiz-completed.ts @@ -1,4 +1,5 @@ import { Payments } from "@/app" +import { PartialResultType } from "@/app/partial-result" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" import { GT } from "@/graphql/index" @@ -26,19 +27,19 @@ const QuizCompletedMutation = GT.Field< resolve: async (_, args, { domainAccount }) => { const { id } = args.input - const res = await Payments.addEarn({ + const { result, error, type } = await Payments.addEarn({ quizQuestionId: id, accountId: domainAccount.id, }) - if (res instanceof Error) { - return { errors: [mapAndParseErrorForGqlResponse(res)] } + + if (type === PartialResultType.Err) { + return { errors: [mapAndParseErrorForGqlResponse(error)] } } return { - errors: res.rewardPaymentError - ? [mapAndParseErrorForGqlResponse(res.rewardPaymentError)] - : [], - quiz: res.quizQuestion, + errors: error ? [mapAndParseErrorForGqlResponse(error)] : [], + quiz: result.quiz, + rewardPaid: result.rewardPaid, } }, }) diff --git a/core/api/src/graphql/public/schema.graphql b/core/api/src/graphql/public/schema.graphql index b4fef7f1080..fc5efb625ac 100644 --- a/core/api/src/graphql/public/schema.graphql +++ b/core/api/src/graphql/public/schema.graphql @@ -1080,6 +1080,7 @@ input QuizCompletedInput { type QuizCompletedPayload { errors: [Error!]! quiz: Quiz + rewardPaid: Boolean! } type QuizQuestion { diff --git a/core/api/src/graphql/public/types/object/business-account.ts b/core/api/src/graphql/public/types/object/business-account.ts index d323a299009..85146362c5b 100644 --- a/core/api/src/graphql/public/types/object/business-account.ts +++ b/core/api/src/graphql/public/types/object/business-account.ts @@ -25,8 +25,8 @@ import { SAT_PRICE_PRECISION_OFFSET, USD_PRICE_PRECISION_OFFSET, } from "@/domain/fiat" -import { CouldNotFindTransactionsForAccountError } from "@/domain/errors" import { Accounts, Prices, Wallets } from "@/app" +import { PartialResultType } from "@/app/partial-result" const BusinessAccount = GT.Object({ name: "BusinessAccount", @@ -137,18 +137,15 @@ const BusinessAccount = GT.Object({ walletIds = wallets.map((wallet) => wallet.id) } - const { result, error } = await Accounts.getTransactionsForAccountByWalletIds({ - account: source, - walletIds, - paginationArgs, - }) - if (error instanceof Error) { - throw mapError(error) - } + const { result, error, type } = + await Accounts.getTransactionsForAccountByWalletIds({ + account: source, + walletIds, + paginationArgs, + }) - if (!result?.slice) { - const nullError = new CouldNotFindTransactionsForAccountError() - throw mapError(nullError) + if (type !== PartialResultType.Ok) { + throw mapError(error) } return connectionFromPaginatedArray( diff --git a/core/api/src/graphql/public/types/object/consumer-account.ts b/core/api/src/graphql/public/types/object/consumer-account.ts index a6d602c4c1d..67590279076 100644 --- a/core/api/src/graphql/public/types/object/consumer-account.ts +++ b/core/api/src/graphql/public/types/object/consumer-account.ts @@ -17,7 +17,6 @@ import { SAT_PRICE_PRECISION_OFFSET, USD_PRICE_PRECISION_OFFSET, } from "@/domain/fiat" -import { CouldNotFindTransactionsForAccountError } from "@/domain/errors" import { GT } from "@/graphql/index" import { mapError } from "@/graphql/error-map" @@ -36,6 +35,7 @@ import DisplayCurrency from "@/graphql/shared/types/scalar/display-currency" import { WalletsRepository } from "@/services/mongoose" import { listEndpoints } from "@/app/callback" +import { PartialResultType } from "@/app/partial-result" const ConsumerAccount = GT.Object({ name: "ConsumerAccount", @@ -198,21 +198,17 @@ const ConsumerAccount = GT.Object({ walletIds = wallets.map((wallet) => wallet.id) } - const { result, error } = await Accounts.getTransactionsForAccountByWalletIds({ - account: source, - walletIds, - paginationArgs, - }) + const { result, error, type } = + await Accounts.getTransactionsForAccountByWalletIds({ + account: source, + walletIds, + paginationArgs, + }) - if (error instanceof Error) { + if (type !== PartialResultType.Ok) { throw mapError(error) } - if (!result?.slice) { - const nullError = new CouldNotFindTransactionsForAccountError() - throw mapError(nullError) - } - return connectionFromPaginatedArray( result.slice, result.total, diff --git a/core/api/src/graphql/public/types/payload/quiz-completed.ts b/core/api/src/graphql/public/types/payload/quiz-completed.ts index 0e6ba195807..8f4b7d957e7 100644 --- a/core/api/src/graphql/public/types/payload/quiz-completed.ts +++ b/core/api/src/graphql/public/types/payload/quiz-completed.ts @@ -12,6 +12,9 @@ const QuizCompletedPayload = GT.Object({ quiz: { type: Quiz, }, + rewardPaid: { + type: GT.NonNull(GT.Boolean), + }, }), }) diff --git a/core/api/src/graphql/shared/types/object/btc-wallet.ts b/core/api/src/graphql/shared/types/object/btc-wallet.ts index 15c8d61d85d..33e93d53303 100644 --- a/core/api/src/graphql/shared/types/object/btc-wallet.ts +++ b/core/api/src/graphql/shared/types/object/btc-wallet.ts @@ -24,6 +24,7 @@ import { mapError } from "@/graphql/error-map" import { Wallets } from "@/app" import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared" +import { PartialResultType } from "@/app/partial-result" const BtcWallet = GT.Object({ name: "BTCWallet", @@ -73,17 +74,15 @@ const BtcWallet = GT.Object({ throw paginationArgs } - const { result, error } = await Wallets.getTransactionsForWallets({ + const { result, error, type } = await Wallets.getTransactionsForWallets({ wallets: [source], paginationArgs, }) - if (error instanceof Error) { + + if (type !== PartialResultType.Ok) { throw mapError(error) } - // Non-null signal to type checker; consider fixing in PartialResult type - if (!result?.slice) throw error - return connectionFromPaginatedArray( result.slice, result.total, @@ -110,18 +109,17 @@ const BtcWallet = GT.Object({ const { address } = args if (address instanceof Error) throw address - const { result, error } = await Wallets.getTransactionsForWalletsByAddresses({ - wallets: [source], - addresses: [address], - paginationArgs, - }) - if (error instanceof Error) { + const { result, error, type } = + await Wallets.getTransactionsForWalletsByAddresses({ + wallets: [source], + addresses: [address], + paginationArgs, + }) + + if (type !== PartialResultType.Ok) { throw mapError(error) } - // Non-null signal to type checker; consider fixing in PartialResult type - if (!result?.slice) throw error - return connectionFromPaginatedArray( result.slice, result.total, diff --git a/core/api/src/graphql/shared/types/object/usd-wallet.ts b/core/api/src/graphql/shared/types/object/usd-wallet.ts index 93291623f84..87c3199430b 100644 --- a/core/api/src/graphql/shared/types/object/usd-wallet.ts +++ b/core/api/src/graphql/shared/types/object/usd-wallet.ts @@ -24,6 +24,7 @@ import { mapError } from "@/graphql/error-map" import { Wallets } from "@/app" import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared" +import { PartialResultType } from "@/app/partial-result" const UsdWallet = GT.Object({ name: "UsdWallet", @@ -72,17 +73,15 @@ const UsdWallet = GT.Object({ throw paginationArgs } - const { result, error } = await Wallets.getTransactionsForWallets({ + const { result, error, type } = await Wallets.getTransactionsForWallets({ wallets: [source], paginationArgs, }) - if (error instanceof Error) { + + if (type !== PartialResultType.Ok) { throw mapError(error) } - // Non-null signal to type checker; consider fixing in PartialResult type - if (!result?.slice) throw error - return connectionFromPaginatedArray( result.slice, result.total, @@ -108,18 +107,17 @@ const UsdWallet = GT.Object({ const { address } = args if (address instanceof Error) throw address - const { result, error } = await Wallets.getTransactionsForWalletsByAddresses({ - wallets: [source], - addresses: [address], - paginationArgs, - }) - if (error instanceof Error) { + const { result, error, type } = + await Wallets.getTransactionsForWalletsByAddresses({ + wallets: [source], + addresses: [address], + paginationArgs, + }) + + if (type !== PartialResultType.Ok) { throw mapError(error) } - // Non-null signal to type checker; consider fixing in PartialResult type - if (!result?.slice) throw error - return connectionFromPaginatedArray( result.slice, result.total, diff --git a/core/api/src/services/tracing.ts b/core/api/src/services/tracing.ts index 1d682e7d05d..bc9dd446b84 100644 --- a/core/api/src/services/tracing.ts +++ b/core/api/src/services/tracing.ts @@ -430,8 +430,7 @@ export const wrapToRunInSpan = < const ret = fn(...args) if (ret instanceof Error) recordException(span, ret) const partialRet = ret as PartialResult - if (partialRet?.partialResult && partialRet?.error) - recordException(span, partialRet.error) + if (partialRet?.error) recordException(span, partialRet.error) span.end() return ret } catch (error) { @@ -487,8 +486,7 @@ export const wrapAsyncToRunInSpan = < const ret = await fn(...args) if (ret instanceof Error) recordException(span, ret) const partialRet = ret as PartialResult - if (partialRet?.partialResult && partialRet?.error) - recordException(span, partialRet.error) + if (partialRet?.error) recordException(span, partialRet.error) span.end() return ret } catch (error) {