Skip to content

Commit

Permalink
fix(swap): handle errors from order posting API (#3224)
Browse files Browse the repository at this point in the history
* fix(swap): handle errors from order posting API

* chore: capitalize first letter of error

* chore: move capitalizeFirstLetter to lib
  • Loading branch information
shoom3301 authored Oct 16, 2023
1 parent 29e5178 commit 99df6d2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
14 changes: 4 additions & 10 deletions apps/cowswap-frontend/src/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CowEnv,
EnrichedOrder,
NativePriceResponse,
OrderBookApiError,
OrderKind,
OrderQuoteRequest,
OrderQuoteResponse,
Expand All @@ -25,9 +24,11 @@ import { LegacyFeeQuoteParams as FeeQuoteParams } from 'legacy/state/price/types

import { getAppData } from 'modules/appData'

import { ApiErrorCodes, ApiErrorObject } from 'api/gnosisProtocol/errors/OperatorError'
import { ApiErrorCodes } from 'api/gnosisProtocol/errors/OperatorError'
import GpQuoteError, { GpQuoteErrorDetails, mapOperatorErrorToQuoteError } from 'api/gnosisProtocol/errors/QuoteError'

import { getIsOrderBookTypedError } from './getIsOrderBookTypedError'

function getProfileUrl(): Partial<Record<ChainId, string>> {
if (isLocal || isDev || isPr || isBarn) {
return {
Expand Down Expand Up @@ -165,7 +166,7 @@ export async function getQuote(params: FeeQuoteParams): Promise<OrderQuoteRespon
}

return orderBookApi.getQuote(quoteParams, { chainId }).catch((error) => {
if (isOrderbookTypedError(error)) {
if (getIsOrderBookTypedError(error)) {
const errorObject = mapOperatorErrorToQuoteError(error.body)

return Promise.reject(errorObject ? new GpQuoteError(errorObject) : error)
Expand All @@ -175,13 +176,6 @@ export async function getQuote(params: FeeQuoteParams): Promise<OrderQuoteRespon
})
}

export type OrderbookTypedError = OrderBookApiError<ApiErrorObject>

function isOrderbookTypedError(e: any): e is OrderbookTypedError {
const error = e as OrderbookTypedError
return error.body.errorType !== undefined && error.body.description !== undefined
}

export async function getOrder(chainId: ChainId, orderId: string, env?: CowEnv): Promise<EnrichedOrder | null> {
const contextOverride = {
chainId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { OrderBookApiError } from '@cowprotocol/cow-sdk'

import { ApiErrorObject } from './errors/OperatorError'

export type OrderBookTypedError = OrderBookApiError<ApiErrorObject>

export function getIsOrderBookTypedError(e: any): e is OrderBookTypedError {
const error = e as OrderBookTypedError
return error.body.errorType !== undefined && error.body.description !== undefined
}
1 change: 1 addition & 0 deletions apps/cowswap-frontend/src/api/gnosisProtocol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as realApi from './api'
import * as mockApi from './mock'

export type { UnsupportedToken, OrderID } from './api'
export { getIsOrderBookTypedError } from './getIsOrderBookTypedError'

const useMock = process.env.REACT_APP_MOCK === 'true'

Expand Down
12 changes: 10 additions & 2 deletions apps/cowswap-frontend/src/modules/trade/utils/swapErrorHelper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { getProviderErrorMessage, isRejectRequestProviderError } from '@cowprotocol/common-utils'
import { capitalizeFirstLetter, getProviderErrorMessage, isRejectRequestProviderError } from '@cowprotocol/common-utils'

import { getIsOrderBookTypedError } from 'api/gnosisProtocol'

export const USER_SWAP_REJECTED_ERROR = 'User rejected signing the order'

export function getSwapErrorMessage(error: Error): string {
if (isRejectRequestProviderError(error)) {
return USER_SWAP_REJECTED_ERROR
} else {
return getProviderErrorMessage(error)
const defaultErrorMessage = getProviderErrorMessage(error)

if (getIsOrderBookTypedError(error)) {
return capitalizeFirstLetter(error.body?.description) || defaultErrorMessage
}

return defaultErrorMessage
}
}
5 changes: 5 additions & 0 deletions libs/common-utils/src/capitalizeFirstLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function capitalizeFirstLetter(str: string): string {
if (!str) return str

return str.charAt(0).toUpperCase() + str.slice(1)
}
1 change: 1 addition & 0 deletions libs/common-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export * from './getRandomInt'
export * from './isEnoughAmount'
export * from './tryParseFractionalAmount'
export * from './maxAmountSpend'
export * from './capitalizeFirstLetter'
export * from './jotai/atomWithPartialUpdate'

0 comments on commit 99df6d2

Please sign in to comment.