Skip to content

Commit

Permalink
Emit errors in handleShape (#70)
Browse files Browse the repository at this point in the history
* Emit errors in handleShape

* Add changeset
  • Loading branch information
nerdenough authored Mar 10, 2023
1 parent b542459 commit f46635a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-clouds-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crystallize/import-utilities': patch
---

Emit shape creation errors
19 changes: 16 additions & 3 deletions src/bootstrap-tenant/bootstrapper/shapes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const shouldDefer = (data: Shape): boolean => {
async function handleShape(
data: Shape,
context: BootstrapperContext,
onUpdate: (t: AreaUpdate) => void,
isDeferred = false
): Promise<Status> {
const s = { ...data }
Expand All @@ -92,16 +93,28 @@ async function handleShape(
)

if (!result) {
onUpdate({
error: {
code: 'CANNOT_HANDLE_SHAPE',
message: 'API did not return any result',
},
})
return Status.error
}
if (defer) {
return Status.deferred
}
return Status.updated
} catch (err) {
} catch (err: any) {
if (context.config.logLevel === 'verbose') {
console.error(err)
}
onUpdate({
error: {
code: 'CANNOT_HANDLE_SHAPE',
message: err.message,
},
})
return Status.error
}
}
Expand Down Expand Up @@ -208,7 +221,7 @@ export async function setShapes({
}
}

const result = await handleShape(data, context)
const result = await handleShape(data, context, onUpdate)
if (result === 'deferred') {
deferredShapes.push(data)
} else {
Expand All @@ -223,7 +236,7 @@ export async function setShapes({

for (let i = 0; i < deferredShapes.length; i++) {
const shape = deferredShapes[i]
const result = await handleShape(shape, context, true)
const result = await handleShape(shape, context, onUpdate, true)
finished++
onUpdate({
progress: finished / spec.shapes.length,
Expand Down
3 changes: 2 additions & 1 deletion src/bootstrap-tenant/bootstrapper/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const EVENT_NAMES = {
}

export type EVENT_NAMES_KEYS = keyof typeof EVENT_NAMES
export type EVENT_NAMES_VALUES = typeof EVENT_NAMES[EVENT_NAMES_KEYS]
export type EVENT_NAMES_VALUES = (typeof EVENT_NAMES)[EVENT_NAMES_KEYS]

export interface AreaWarning {
message: string
Expand All @@ -63,6 +63,7 @@ export interface AreaError {
code:
| 'UPLOAD_FAILED'
| 'SHAPE_ID_MISSING'
| 'CANNOT_HANDLE_SHAPE'
| 'CANNOT_HANDLE_ITEM'
| 'CANNOT_HANDLE_PRODUCT'
| 'CANNOT_HANDLE_ITEM_RELATION'
Expand Down

0 comments on commit f46635a

Please sign in to comment.