Skip to content

Commit

Permalink
add GameMoneyError class
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpain committed May 21, 2022
1 parent da1218d commit 4feb903
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
31 changes: 20 additions & 11 deletions src/examples/checkout/create.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { v4 as uuidv4 } from 'uuid'
import gm from '../gm.js'
import { GameMoneyError } from '../../index.js'

const projectCheckoutId = uuidv4()

const response = await gm.createCheckout({
projectId: projectCheckoutId,
user: '1',
ip: '72.14.192.0',
amount: 200.5,
wallet: '89123456789',
type: 'qiwi',
description: 'Payout for user account 250115125',
add_some_field: 'some value',
})
try {
const response = await gm.createCheckout({
projectId: projectCheckoutId,
user: '1',
ip: '72.14.192.0',
amount: 200.5,
wallet: '89123456789',
type: 'qiwi',
description: 'Payout for user account 250115125',
add_some_field: 'some value',
})

console.log({ projectCheckoutId, response })
console.log({ projectCheckoutId, response })
} catch (error: unknown) {
if (error instanceof GameMoneyError) {
console.log('Error from GameMoney API:', error.message)
} else {
console.log('another error:', error)
}
}
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,20 @@ export interface TransferNotification {
}

type Payload = Record<string, unknown>

export class GameMoneyError extends Error {
constructor(message: string) {
super(message)
this.name = 'GameMoneyError'
}
}
export default class GameMoney {
private readonly config: Config
private readonly got = got.extend({
prefixUrl: 'https://paygate.gamemoney.com',
responseType: 'json',
})

constructor(config: Config) {
constructor(private readonly config: Config) {
this.config = config
}

Expand Down Expand Up @@ -456,7 +462,7 @@ export default class GameMoney {
}

if (response.state === 'error') {
throw new Error(response.error)
throw new GameMoneyError(response.error!)
}

return response
Expand Down

0 comments on commit 4feb903

Please sign in to comment.