Skip to content

Commit

Permalink
Throw proper Airtable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thgh committed Jan 25, 2020
1 parent 445e102 commit 5541ec2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
21 changes: 13 additions & 8 deletions airtable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function create(env, tableName, fields) {
body: JSON.stringify({ fields }),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -37,7 +37,7 @@ export async function find(env, tableName, id) {
headers: headers(env.key),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -53,11 +53,13 @@ export async function select(env, tableName, filter) {
const body = await fetch(app(env.app) + tableName + '?' + serialize(filter), {
headers: headers(env.key),
}).then(r => r.json())
const { records } = body
const { error, records } = body
if (error) {
throw new Error(error.message)
}
if (records) {
return records.map(unpack)
}
console.error(body)
return []
}

Expand All @@ -66,7 +68,10 @@ export async function selectAll(env, tableName, filter, prepend = []) {
const body = await fetch(app(env.app) + tableName + '?' + serialize(filter), {
headers: headers(env.key),
}).then(r => r.json())
const { offset, records } = body
const { error, offset, records } = body
if (error) {
throw new Error(error.message)
}
if (offset) {
return selectAll(
env,
Expand All @@ -90,7 +95,7 @@ export async function update(env, tableName, id, fields) {
body: JSON.stringify({ fields }),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -101,8 +106,8 @@ export async function remove(env, tableName, id) {
method: 'DELETE',
headers: headers(env.key),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
if (body.error.message) {
throw new Error(body.error.message)
}
return unpack(body)
}
Expand Down
18 changes: 12 additions & 6 deletions airtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function create(
body: JSON.stringify({ fields }),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -45,7 +45,7 @@ export async function find(
headers: headers(env.key),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -69,7 +69,10 @@ export async function select(
const body = await fetch(app(env.app) + tableName + '?' + serialize(filter), {
headers: headers(env.key),
}).then(r => r.json())
const { records } = body
const { error, records } = body
if (error) {
throw new Error(error.message)
}
if (records) {
return records.map(unpack)
}
Expand All @@ -87,7 +90,10 @@ export async function selectAll(
const body = await fetch(app(env.app) + tableName + '?' + serialize(filter), {
headers: headers(env.key),
}).then(r => r.json())
const { offset, records } = body
const { error, offset, records } = body
if (error) {
throw new Error(error.message)
}
if (offset) {
return selectAll(
env,
Expand Down Expand Up @@ -116,7 +122,7 @@ export async function update(
body: JSON.stringify({ fields }),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand All @@ -132,7 +138,7 @@ export async function remove(
headers: headers(env.key),
}).then(r => r.json())
if (body.error) {
throw new Error(body.error)
throw new Error(body.error.message)
}
return unpack(body)
}
Expand Down

0 comments on commit 5541ec2

Please sign in to comment.