feat: Normalize known DB errors to a MedusaError when possible (#6922)
Before we would swallow the error and return a generic error to the user. This will provide more information to the caller if it is one of the known errors.
This commit is contained in:
@@ -106,9 +106,7 @@ export default class CustomerModuleService<
|
||||
| CustomerTypes.CreateCustomerDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CustomerTypes.CustomerDTO | CustomerTypes.CustomerDTO[]> {
|
||||
const customers = await this.create_(dataOrArray, sharedContext).catch(
|
||||
this.handleDbErrors
|
||||
)
|
||||
const customers = await this.create_(dataOrArray, sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerDTO[]
|
||||
@@ -351,9 +349,7 @@ export default class CustomerModuleService<
|
||||
): Promise<
|
||||
CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
|
||||
> {
|
||||
const addresses = await this.addAddresses_(data, sharedContext).catch(
|
||||
this.handleDbErrors
|
||||
)
|
||||
const addresses = await this.addAddresses_(data, sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerAddressDTO[]
|
||||
@@ -434,7 +430,7 @@ export default class CustomerModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await this.flush(sharedContext).catch(this.handleDbErrors)
|
||||
await this.flush(sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerAddressDTO[]
|
||||
@@ -475,25 +471,4 @@ export default class CustomerModuleService<
|
||||
const em = (context.manager ?? context.transactionManager) as EntityManager
|
||||
await em.flush()
|
||||
}
|
||||
|
||||
private async handleDbErrors(err: any) {
|
||||
if (isDuplicateError(err)) {
|
||||
switch (err.constraint) {
|
||||
case UNIQUE_CUSTOMER_SHIPPING_ADDRESS:
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.DUPLICATE_ERROR,
|
||||
"A default shipping address already exists"
|
||||
)
|
||||
case UNIQUE_CUSTOMER_BILLING_ADDRESS:
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.DUPLICATE_ERROR,
|
||||
"A default billing address already exists"
|
||||
)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user