chore(medusa, workflows-sdk): Workflow default to throw the first error (#7442)

**What**
Currently when a workflow fail it will throw an error which contains the messages of all error thrown durring the workflow lifetime. Therefore, in our cases we are always preventing workflow to throw and grab the first error that we then re throw.
This pr eliminate that need by throwing back the first error among the error thrown by a workflow as it is the main case. In case someone need a special handling they can still set the option throwOnError to false and handle the error the way they need
This commit is contained in:
Adrien de Peretti
2024-05-24 12:55:05 +00:00
committed by GitHub
parent 16651f9849
commit fff1b3ef9c
109 changed files with 147 additions and 773 deletions
@@ -37,18 +37,13 @@ export const POST = async (
res: MedusaResponse
) => {
const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
const { errors } = await updateAddresses.run({
await updateAddresses.run({
input: {
selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -65,15 +60,10 @@ export const DELETE = async (
const id = req.params.address_id
const deleteAddress = deleteCustomerAddressesWorkflow(req.scope)
const { errors } = await deleteAddress.run({
await deleteAddress.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -49,15 +49,10 @@ export const POST = async (
},
]
const { errors } = await createAddresses.run({
await createAddresses.run({
input: { addresses },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
customerId,
req.scope,
@@ -35,18 +35,13 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>,
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const { errors } = await updateCustomersWorkflow(req.scope).run({
await updateCustomersWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
req.params.id,
req.scope,
@@ -62,15 +57,10 @@ export const DELETE = async (
const id = req.params.id
const deleteCustomers = deleteCustomersWorkflow(req.scope)
const { errors } = await deleteCustomers.run({
await deleteCustomers.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "customer",
@@ -50,15 +50,10 @@ export const POST = async (
},
]
const { result, errors } = await createCustomers.run({
const { result } = await createCustomers.run({
input: { customersData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customer = await refetchCustomer(
result[0].id,
req.scope,