Skip to content

Commit

Permalink
Merge pull request #158 from Akord-com/return-full-errors
Browse files Browse the repository at this point in the history
feat: return full error object for list/batch actions
  • Loading branch information
wkolod authored Jul 13, 2023
2 parents ca7c332 + a052a06 commit beb05e0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/core/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class BatchService extends Service {
}

const data = [] as { stackId: string, transactionId: string, object: Stack }[];
const errors = [] as { name: string, message: string }[];
const errors = [] as { name: string, message: string, error: Error }[];

if (options.progressHook) {
const onProgress = options.progressHook
Expand Down Expand Up @@ -150,8 +150,8 @@ class BatchService extends Service {
if (options.onStackCreated) {
await options.onStackCreated(stackResponse.object);
}
} catch (e) {
errors.push({ name: item.name, message: e.toString() })
} catch (error) {
errors.push({ name: item.name, message: error.toString(), error })
};
}))
if (options.cancelHook?.signal.aborted) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ProfileService extends Service {
})
public async update(name: string, avatar: ArrayBuffer): Promise<{
transactions: { id: string, transactionId: string }[],
errors: { id: string, error: any }[]
errors: { id: string, error: Error }[]
}> {
// update profile
const user = await this.api.getUser();
Expand Down
4 changes: 2 additions & 2 deletions src/core/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,13 @@ class Service {
}

protected async handleListErrors<T>(originalItems: Array<T>, promises: Array<Promise<T>>)
: Promise<{ items: Array<T>, errors: Array<{ id: string, error: string }> }> {
: Promise<{ items: Array<T>, errors: Array<{ id: string, error: Error }> }> {
const results = await Promise.all(promises.map(p => p.catch(e => e)));
const items = results.filter(result => !(result instanceof Error));
const errors = results
.map((result, index) => ({ result, index }))
.filter((mapped) => mapped.result instanceof Error)
.map((filtered) => ({ id: (<any>originalItems[filtered.index]).id, error: filtered.result.message }));
.map((filtered) => ({ id: (<any>originalItems[filtered.index]).id, error: filtered.result }));
return { items, errors };
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/batch-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Stack } from "./node"

export interface BatchStackCreateResponse {
data: Array<{ stackId: string, transactionId: string, object: Stack }>
errors: Array<{ name: string, message: string }>
errors: Array<{ name: string, message: string, error: Error }>
cancelled: number
}

export interface BatchMembershipInviteResponse {
data: Array<{ membershipId: string, transactionId: string }>
errors: Array<{ email: string, message: string }>
errors: Array<{ email: string, message: string, error: Error }>
}
2 changes: 1 addition & 1 deletion src/types/paginated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PAGINATION_HEADER = 'next-page'
export type Paginated<T> = {
items: Array<T>
nextToken: string
errors?: Array<{ id: string, error: string }>
errors?: Array<{ id: string, error: Error }>
}

export const isPaginated = (response: AxiosResponse) => {
Expand Down

0 comments on commit beb05e0

Please sign in to comment.