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:
+10
-4
@@ -106,7 +106,7 @@ moduleIntegrationTestRunner({
|
||||
],
|
||||
}
|
||||
await expect(service.create(customerData)).rejects.toThrow(
|
||||
"A default shipping address already exists"
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
|
||||
@@ -727,7 +727,9 @@ moduleIntegrationTestRunner({
|
||||
country_code: "US",
|
||||
is_default_shipping: true,
|
||||
})
|
||||
).rejects.toThrow("A default shipping address already exists")
|
||||
).rejects.toThrow(
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
|
||||
it("should only be possible to add one default billing address per customer", async () => {
|
||||
@@ -761,7 +763,9 @@ moduleIntegrationTestRunner({
|
||||
country_code: "US",
|
||||
is_default_billing: true,
|
||||
})
|
||||
).rejects.toThrow("A default billing address already exists")
|
||||
).rejects.toThrow(
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -899,7 +903,9 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await expect(
|
||||
service.updateAddresses(address.id, { is_default_shipping: true })
|
||||
).rejects.toThrow("A default shipping address already exists")
|
||||
).rejects.toThrow(
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -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