Skip to content

Commit 6fd308f

Browse files
authored
feat(PR-40): throw custom error (#103)
Use custom `Typeform.ApiError` class to throw error with more details.
1 parent a826100 commit 6fd308f

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/bin.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inspect } from 'util'
22

3-
import { createClient } from './index'
3+
import { createClient, Typeform } from './index'
44

55
const print = (message: string) => {
66
// eslint-disable-next-line no-console
@@ -57,6 +57,14 @@ typeformAPI[property][method](parsedParams)
5757
.then((result: Object) => {
5858
print(inspect(result, { showHidden: false, depth: null, colors: true }))
5959
})
60-
.catch((err: Error) => {
61-
print(`Error: ${err.message}`)
60+
.catch((err: Typeform.ApiError) => {
61+
const detailsString = inspect(err.details, {
62+
showHidden: false,
63+
depth: null,
64+
colors: true,
65+
})
66+
print(`---------------------------------------------`)
67+
print(`Error: ${err.code}: ${err.message}`)
68+
print(`Details: ${detailsString}`)
69+
print(`---------------------------------------------`)
6270
})

src/create-client.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const clientConstructor = ({
2727
const authorization = token ? { Authorization: `bearer ${token}` } : {}
2828
const requestParameters = { ...options, ...otherArgs }
2929

30-
// @ts-ignore
3130
return axios({
3231
url: requestUrl,
3332
...requestParameters,
@@ -41,16 +40,12 @@ export const clientConstructor = ({
4140
})
4241
.then((response) => response.data)
4342
.catch((error) => {
44-
if (
45-
error &&
46-
error.response &&
47-
error.response.data &&
48-
error.response.data.description
49-
) {
50-
throw new Error(error.response.data.description)
51-
} else {
52-
throw new Error("Couldn't make request")
53-
}
43+
const {
44+
code,
45+
description = "Couldn't make request",
46+
details,
47+
} = error?.response?.data || {}
48+
throw new Typeform.ApiError(code, description, details)
5449
})
5550
},
5651
}

src/typeform-types.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export namespace Typeform {
201201
/**
202202
* Type of value the condition object refers to.
203203
*/
204-
type?: 'field' | 'hidden' | 'variable' | 'constant' | 'end'| 'choice'
204+
type?: 'field' | 'hidden' | 'variable' | 'constant' | 'end' | 'choice'
205205
/**
206206
* Value to check for in the "type" field to evaluate with the operator.
207207
*/
@@ -212,13 +212,13 @@ export namespace Typeform {
212212
* Conditions for a logic jump can be combined using the `and` and `or` operators
213213
*/
214214
export interface AndOrOperator {
215-
/**
216-
* Operator for the condition.
217-
*/
215+
/**
216+
* Operator for the condition.
217+
*/
218218
op?: 'and' | 'or'
219-
/**
220-
* Object that defines the field type and value to evaluate with the operator.
221-
*/
219+
/**
220+
* Object that defines the field type and value to evaluate with the operator.
221+
*/
222222
vars: Array<AndOrOperator | Condition>
223223
}
224224
/**
@@ -1378,4 +1378,14 @@ export namespace Typeform {
13781378
role: 'owner' | 'member'
13791379
}[]
13801380
}
1381+
export class ApiError extends Error {
1382+
code: string
1383+
details: Object[]
1384+
1385+
constructor(code: string, message: string, details: Object[]) {
1386+
super(message)
1387+
this.code = code
1388+
this.details = details
1389+
}
1390+
}
13811391
}

0 commit comments

Comments
 (0)