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:
committed by
GitHub
parent
16651f9849
commit
fff1b3ef9c
@@ -10,7 +10,7 @@ export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminRevokeApiKeyType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { errors } = await revokeApiKeysWorkflow(req.scope).run({
|
||||
await revokeApiKeysWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
revoke: {
|
||||
@@ -18,13 +18,8 @@ export const POST = async (
|
||||
revoked_by: req.auth_context.actor_id,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const apiKey = await refetchApiKey(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
|
||||
@@ -27,18 +27,13 @@ export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminUpdateApiKeyType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { errors } = await updateApiKeysWorkflow(req.scope).run({
|
||||
await updateApiKeysWorkflow(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 apiKey = await refetchApiKey(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
@@ -54,15 +49,10 @@ export const DELETE = async (
|
||||
) => {
|
||||
const id = req.params.id
|
||||
|
||||
const { errors } = await deleteApiKeysWorkflow(req.scope).run({
|
||||
await deleteApiKeysWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "api_key",
|
||||
|
||||
@@ -21,19 +21,14 @@ export const POST = async (
|
||||
)
|
||||
}
|
||||
|
||||
const { errors } = await linkSalesChannelsToApiKeyWorkflow(req.scope).run({
|
||||
await linkSalesChannelsToApiKeyWorkflow(req.scope).run({
|
||||
input: {
|
||||
id: req.params.id,
|
||||
add,
|
||||
remove,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const updatedApiKey = await refetchApiKey(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createApiKeysWorkflow } from "@medusajs/core-flows"
|
||||
import {createApiKeysWorkflow} from "@medusajs/core-flows"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { AdminCreateApiKeyType } from "./validators"
|
||||
import {AdminCreateApiKeyType} from "./validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
@@ -45,15 +45,10 @@ export const POST = async (
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createApiKeysWorkflow(req.scope).run({
|
||||
const { result } = await createApiKeysWorkflow(req.scope).run({
|
||||
input: { api_keys: input },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
// We should not refetch the api key here, as we need to show the secret key in the response (and never again)
|
||||
// And the only time we get to see the secret, is when we create it
|
||||
res.status(200).json({ api_key: result[0] })
|
||||
|
||||
Reference in New Issue
Block a user