-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Normalize known DB errors to a MedusaError when possible
- Loading branch information
Showing
8 changed files
with
240 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/utils": minor | ||
--- | ||
|
||
Return normalized DB errors from the mikro ORM repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
ForeignKeyConstraintViolationException, | ||
InvalidFieldNameException, | ||
NotFoundError, | ||
NotNullConstraintViolationException, | ||
UniqueConstraintViolationException, | ||
} from "@mikro-orm/core" | ||
import { MedusaError, upperCaseFirst } from "../../common" | ||
|
||
export const dbErrorMapper = (err: Error) => { | ||
if (err instanceof NotFoundError) { | ||
throw new MedusaError(MedusaError.Types.NOT_FOUND, err.message) | ||
} | ||
|
||
if (err instanceof UniqueConstraintViolationException) { | ||
const info = getConstraintInfo(err) | ||
if (!info) { | ||
throw err | ||
} | ||
|
||
throw new MedusaError( | ||
MedusaError.Types.INVALID_DATA, | ||
`${upperCaseFirst(info.table)} with ${info.keys | ||
.map((key, i) => `${key}: ${info.values[i]}`) | ||
.join(", ")} already exists.` | ||
) | ||
} | ||
|
||
if (err instanceof NotNullConstraintViolationException) { | ||
throw new MedusaError( | ||
MedusaError.Types.INVALID_DATA, | ||
`Cannot set field '${(err as any).column}' of ${upperCaseFirst( | ||
(err as any).table | ||
)} to null` | ||
) | ||
} | ||
|
||
if (err instanceof InvalidFieldNameException) { | ||
const userFriendlyMessage = err.message.match(/(column.*)/)?.[0] | ||
throw new MedusaError( | ||
MedusaError.Types.INVALID_DATA, | ||
userFriendlyMessage ?? err.message | ||
) | ||
} | ||
|
||
if (err instanceof ForeignKeyConstraintViolationException) { | ||
const info = getConstraintInfo(err) | ||
throw new MedusaError( | ||
MedusaError.Types.INVALID_DATA, | ||
`You tried to set relationship ${info?.keys.map( | ||
(key, i) => `${key}: ${info.values[i]}` | ||
)}, but such entity does not exist` | ||
) | ||
} | ||
|
||
throw err | ||
} | ||
|
||
const getConstraintInfo = (err: any) => { | ||
const detail = err.detail as string | ||
if (!detail) { | ||
return null | ||
} | ||
|
||
const [keys, values] = detail.match(/\([^\(.]*\)/g) || [] | ||
|
||
if (!keys || !values) { | ||
return null | ||
} | ||
|
||
return { | ||
table: err.table.split("_").join(" "), | ||
keys: keys | ||
.substring(1, keys.length - 1) | ||
.split(",") | ||
.map((k) => k.trim()), | ||
values: values | ||
.substring(1, values.length - 1) | ||
.split(",") | ||
.map((v) => v.trim()), | ||
} | ||
} |
Oops, something went wrong.