Skip to content

Commit

Permalink
fix(rem): throws (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilbe27 authored Nov 1, 2023
1 parent 6a47609 commit 6e5ea8f
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 26 deletions.
9 changes: 4 additions & 5 deletions packages/indexer-nibi/src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ export const convertObjectToPropertiesString = (obj: any) => {
export const cleanResponse = async (rawResp: Response) => {
const respJson = await rawResp.json().catch((err) => {
console.error(err)
return undefined
})
// console.debug("DEBUG respJson: %o", respJson)

if (!rawResp.ok || !respJson) {
throw new Error(`${JSON.stringify(respJson)}`)
} else if (respJson.data) {
if (respJson?.data) {
return respJson.data
} else {
return respJson
}

return respJson
}

export const gqlQuery = <T>(
Expand Down
8 changes: 3 additions & 5 deletions packages/indexer-nibi/src/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,22 +1045,20 @@ describe("gql cleanResponse", () => {
expect(result).toEqual({ key: "value" })
})

test("should throw an error if rawResp is not ok", async () => {
test("should return an error if rawResp is not ok", async () => {
const error = { error: "Error message" }
const rawResp = {
ok: false,
json: () => Promise.resolve(error),
} as Response
await expect(cleanResponse(rawResp)).rejects.toThrowError(
`${JSON.stringify(error)}`
)
expect(await cleanResponse(rawResp)).toEqual(error)
})

test("should throw an error if unable to parse JSON", async () => {
const rawResp = {
ok: true,
json: () => Promise.reject(new Error("invalid json")),
} as Response
await expect(cleanResponse(rawResp)).rejects.toThrowError(``)
expect(await cleanResponse(rawResp)).toEqual(undefined)
})
})
2 changes: 1 addition & 1 deletion packages/nibijs/docs/classes/StableSwap.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ y()
Calculate x[j] if one makes x[i] = x

Done by solving quadratic equation iteratively.
x*1**2 + x1 * (sum' - (A*n**n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
x*1\*\*2 + x1 * (sum' - (A*n\*\*n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
x_1\*\*2 + b\*x_1 = c

x_1 = (x_1\**2 + c) / (2*x_1 + b)
Expand Down
2 changes: 1 addition & 1 deletion packages/nibijs/src/chain/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const INT_MULT = 1_000_000
/**
* toSdkDec converts the input float string to an sdk.Dec.
* The maximum number of decimal places for an sdk.Dec is 18.
* NOTE: An error is thrown if more decimal digits are provided than the
* NOTE: An error is console'd if more decimal digits are provided than the
* precision, 18.
*
* ref: Reimplementation of cosmos-sdk/types/decimal.go
Expand Down
4 changes: 3 additions & 1 deletion packages/nibijs/src/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export const go = async <T>(promise: Promise<T>) => {
export const assert = (condition: boolean, message?: string) => {
if (!condition) {
const errMsg = message ? `AssertionError: ${message}` : "AssertionError"
throw new Error(errMsg)
console.error(Error(errMsg))
return errMsg
}
return true
}

export interface CoinMap {
Expand Down
4 changes: 2 additions & 2 deletions packages/nibijs/src/chain/useFaucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function useFaucet({
chain: Chain
amts?: { nibi: number; nusd: number; usdt: number }
grecaptcha: string
}): Promise<Response> {
}): Promise<Response | undefined> {
if (!amts) {
// default values
amts = {
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function useFaucet({
body: JSON.stringify({ address, coins, grecaptcha }),
}).catch((err) => {
console.error(err)
throw err
return undefined
})
}

Expand Down
3 changes: 2 additions & 1 deletion packages/nibijs/src/stableswap/stableswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export class StableSwap {
.dividedBy(BigNumber(10).exponentiatedBy(BigNumber(10)))

if (dy.isLessThanOrEqualTo(BigNumber(0))) {
throw new Error("Invalid exchange operation")
console.error(Error("Invalid exchange operation"))
return BigNumber(0)
}

return dy.minus(fee)
Expand Down
4 changes: 2 additions & 2 deletions packages/nibijs/src/test/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe("chain/types", () => {
})

test("custom assert fn", () => {
expect(() => assert(false)).toThrow()
expect(assert(false)).toEqual("AssertionError")
const err = "useful error message"
expect(() => assert(false, err)).toThrow(err)
expect(assert(false, err)).toEqual(`AssertionError: ${err}`)
})
4 changes: 2 additions & 2 deletions packages/nibijs/src/test/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test.skip("faucet utility works", async () => {
chain: TEST_CHAIN,
grecaptcha: "",
})
expect(faucetResp.ok).toBeTruthy()
expect(faucetResp?.ok).toBeTruthy()

const balancesEnd = newCoinMapFromCoins(
await signingClient.getAllBalances(toAddr)
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("useFaucet", () => {
})
})

test("should throw an error if fetch fails", async () => {
test("should return undefined if fetch fails", async () => {
const errorMessage = "Failed to fetch"

jest.mock("cross-fetch", () => ({
Expand Down
7 changes: 3 additions & 4 deletions packages/nibijs/src/test/stableswap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe("stableswap tests", () => {
/"(\[[^"]+\])",(\d+),(\d+),(\d+),(\d+(\.\d+)?),(\d+(\.\d+)?)/
const match = line.match(regex)
if (!match) {
throw new Error("Invalid line format")
console.error(new Error("Invalid line format"))
return
}

const balances = JSON.parse(match[1]) as BigNumber[]
Expand All @@ -40,8 +41,6 @@ describe("stableswap tests", () => {
const amplification = BigNumber(0)
const curveModel = new StableSwap(amplification, balances, BigNumber(0))

expect(() => curveModel.exchange(0, 1, BigNumber(0))).toThrow(
"Invalid exchange operation"
)
expect(curveModel.exchange(0, 1, BigNumber(0))).toEqual(BigNumber(0))
})
})
3 changes: 2 additions & 1 deletion packages/protojs/src/cosmos/base/query/v1beta1/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ var tsProtoGlobalThis: any = (() => {
if (typeof global !== "undefined") {
return global
}
throw "Unable to locate global object"
console.error("Unable to locate global object")
return undefined
})()

function bytesFromBase64(b64: string): Uint8Array {
Expand Down
3 changes: 2 additions & 1 deletion packages/protojs/src/google/protobuf/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ var tsProtoGlobalThis: any = (() => {
if (typeof global !== "undefined") {
return global
}
throw "Unable to locate global object"
console.error("Unable to locate global object")
return undefined
})()

function bytesFromBase64(b64: string): Uint8Array {
Expand Down

1 comment on commit 6e5ea8f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 96%
96.62% (1260/1304) 99.24% (527/531) 90.76% (295/325)

Please sign in to comment.