chore(medusa): Move formatException to the errorHandler to be always applied and not have to apply it manually (#2467)
**What** Move the usage of the formatException to the errorHandler level in order to not have to try catch here and there to apply it. Also make our error handling uniformed and avoid forgetting to apply it. FIXES CORE-721
This commit is contained in:
1 parent
fcfb7d167b
commit
2f00bd100a
8 files changed
+37
-54
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { Logger } from "../../types/global"
|
||||
import { formatException } from "../../utils";
|
||||
|
||||
const QUERY_RUNNER_RELEASED = "QueryRunnerAlreadyReleasedError"
|
||||
const TRANSACTION_STARTED = "TransactionAlreadyStartedError"
|
||||
@@ -18,6 +19,9 @@ export default () => {
|
||||
next: NextFunction
|
||||
) => {
|
||||
const logger: Logger = req.scope.resolve("logger")
|
||||
|
||||
err = formatException(err)
|
||||
|
||||
logger.error(err)
|
||||
|
||||
const errorType = err.type || err.name
|
||||
|
||||
@@ -10,7 +10,6 @@ import { FindConfig, Selector } from "../types/common"
|
||||
import { CustomerGroupUpdate } from "../types/customer-groups"
|
||||
import {
|
||||
buildQuery,
|
||||
formatException,
|
||||
isDefined,
|
||||
isString,
|
||||
PostgresError,
|
||||
@@ -108,26 +107,8 @@ class CustomerGroupService extends TransactionBaseService {
|
||||
)
|
||||
return await cgRepo.addCustomers(id, ids)
|
||||
},
|
||||
async (error: any) => {
|
||||
if (error.code === PostgresError.FOREIGN_KEY_ERROR) {
|
||||
await this.retrieve(id)
|
||||
|
||||
const existingCustomers = await this.customerService_.list({
|
||||
id: ids,
|
||||
})
|
||||
|
||||
const nonExistingCustomers = ids.filter(
|
||||
(cId) => existingCustomers.findIndex((el) => el.id === cId) === -1
|
||||
)
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`The following customer ids do not exist: ${JSON.stringify(
|
||||
nonExistingCustomers.join(", ")
|
||||
)}`
|
||||
)
|
||||
}
|
||||
throw formatException(error)
|
||||
async (e: any) => {
|
||||
await this.handleCreationFail(id, ids, e)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -274,6 +255,32 @@ class CustomerGroupService extends TransactionBaseService {
|
||||
|
||||
return customerGroup
|
||||
}
|
||||
|
||||
private async handleCreationFail(
|
||||
id: string,
|
||||
ids: string[],
|
||||
error: any
|
||||
): Promise<never> {
|
||||
if (error.code === PostgresError.FOREIGN_KEY_ERROR) {
|
||||
await this.retrieve(id)
|
||||
|
||||
const existingCustomers = await this.customerService_.list({
|
||||
id: ids,
|
||||
})
|
||||
|
||||
const nonExistingCustomers = ids.filter(
|
||||
(cId) => existingCustomers.findIndex((el) => el.id === cId) === -1
|
||||
)
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`The following customer ids do not exist: ${JSON.stringify(
|
||||
nonExistingCustomers.join(", ")
|
||||
)}`
|
||||
)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export default CustomerGroupService
|
||||
@@ -10,7 +10,6 @@ import { CustomerRepository } from "../repositories/customer"
|
||||
import { AddressCreatePayload, FindConfig, Selector } from "../types/common"
|
||||
import { CreateCustomerInput, UpdateCustomerInput } from "../types/customers"
|
||||
import { buildQuery, isDefined, setMetadata } from "../utils"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -19,6 +18,7 @@ type InjectedDependencies = {
|
||||
customerRepository: typeof CustomerRepository
|
||||
addressRepository: typeof AddressRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate customers.
|
||||
*/
|
||||
@@ -301,8 +301,7 @@ class CustomerService extends TransactionBaseService {
|
||||
customerId: string,
|
||||
update: UpdateCustomerInput
|
||||
): Promise<Customer> {
|
||||
return await this.atomicPhase_(
|
||||
async (manager) => {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customerRepository = manager.getCustomRepository(
|
||||
this.customerRepository_
|
||||
)
|
||||
@@ -346,12 +345,9 @@ class CustomerService extends TransactionBaseService {
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(CustomerService.Events.UPDATED, updated)
|
||||
|
||||
return updated
|
||||
},
|
||||
async (error) => {
|
||||
throw formatException(error)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,7 +37,6 @@ import {
|
||||
} from "../types/discount"
|
||||
import { buildQuery, setMetadata } from "../utils"
|
||||
import { isFuture, isPast } from "../utils/date-helpers"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import CustomerService from "./customer"
|
||||
import DiscountConditionService from "./discount-condition"
|
||||
@@ -200,7 +199,6 @@ class DiscountService extends TransactionBaseService {
|
||||
"Fixed discounts can have one region"
|
||||
)
|
||||
}
|
||||
try {
|
||||
if (discount.regions) {
|
||||
discount.regions = (await Promise.all(
|
||||
discount.regions.map(async (regionId) =>
|
||||
@@ -230,9 +228,6 @@ class DiscountService extends TransactionBaseService {
|
||||
}
|
||||
|
||||
return result
|
||||
} catch (error) {
|
||||
throw formatException(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
PriceListPriceUpdateInput,
|
||||
UpdatePriceListInput,
|
||||
} from "../types/price-list"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import ProductService from "./product"
|
||||
import RegionService from "./region"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
@@ -119,7 +118,6 @@ class PriceListService extends TransactionBaseService {
|
||||
|
||||
const { prices, customer_groups, includes_tax, ...rest } = priceListObject
|
||||
|
||||
try {
|
||||
const rawPriceList: DeepPartial<PriceList> = {
|
||||
...rest,
|
||||
}
|
||||
@@ -150,9 +148,6 @@ class PriceListService extends TransactionBaseService {
|
||||
return await this.retrieve(priceList.id, {
|
||||
relations: ["prices", "customer_groups"],
|
||||
})
|
||||
} catch (error) {
|
||||
throw formatException(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
UpdateProductCollection,
|
||||
} from "../types/product-collection"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -113,12 +112,8 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
this.productCollectionRepository_
|
||||
)
|
||||
|
||||
try {
|
||||
const productCollection = collectionRepo.create(collection)
|
||||
return await collectionRepo.save(productCollection)
|
||||
} catch (error) {
|
||||
throw formatException(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -183,7 +178,6 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
|
||||
try {
|
||||
const { id } = await this.retrieve(collectionId, { select: ["id"] })
|
||||
|
||||
await productRepo.bulkAddToCollection(productIds, id)
|
||||
@@ -191,9 +185,6 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
return await this.retrieve(id, {
|
||||
relations: ["products"],
|
||||
})
|
||||
} catch (error) {
|
||||
throw formatException(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ import {
|
||||
UpdateProductInput,
|
||||
} from "../types/product"
|
||||
import { buildQuery, isDefined, setMetadata } from "../utils"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -362,7 +361,6 @@ class ProductService extends TransactionBaseService {
|
||||
rest.discountable = false
|
||||
}
|
||||
|
||||
try {
|
||||
let product = productRepo.create(rest)
|
||||
|
||||
if (images?.length) {
|
||||
@@ -414,9 +412,6 @@ class ProductService extends TransactionBaseService {
|
||||
id: result.id,
|
||||
})
|
||||
return result
|
||||
} catch (error) {
|
||||
throw formatException(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ export enum PostgresError {
|
||||
DUPLICATE_ERROR = "23505",
|
||||
FOREIGN_KEY_ERROR = "23503",
|
||||
}
|
||||
export const formatException = (err): Error => {
|
||||
export const formatException = (err): MedusaError => {
|
||||
switch (err.code) {
|
||||
case PostgresError.DUPLICATE_ERROR:
|
||||
return new MedusaError(
|
||||
|
||||
Reference in new issue
Block a user