Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swap): handle errors from order posting API #3224

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { 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 error.body?.description || defaultErrorMessage
shoom3301 marked this conversation as resolved.
Show resolved Hide resolved
}

return defaultErrorMessage
}
}
Loading