1 import { EOL } from "os"
2
3 export const DatabaseErrorCode = {
4 databaseDoesNotExist: "3D000",
5 connectionFailure: "ECONNREFUSED",
6 wrongCredentials: "28000",
7 notFound: "ENOTFOUND",
8 migrationMissing: "42P01",
9 }
10
11 export function handlePostgresDatabaseError(err: any): never {
12 if (DatabaseErrorCode.databaseDoesNotExist === err.code) {
13 throw new Error(
14 `The specified PostgreSQL database does not exist. Please create it and try again.${EOL}${err.message}`
15 )
16 }
17
18 if (DatabaseErrorCode.connectionFailure === err.code) {
19 throw new Error(
20 `Failed to establish a connection to PostgreSQL. Please ensure the following is true and try again:
21 - You have a PostgreSQL database running
22 - You have passed the correct credentials in medusa-config.js
23 - You have formatted the database connection string correctly. See below:
24 "postgres://[username]:[password]@[host]:[port]/[db_name]" - If there is no password, you can omit it from the connection string
25 ${EOL}
26 ${err.message}`
27 )
28 }
29
30 if (DatabaseErrorCode.wrongCredentials === err.code) {
31 throw new Error(
32 `The specified credentials does not exists for the specified PostgreSQL database.${EOL}${err.message}`
33 )
34 }
35
36 if (DatabaseErrorCode.notFound === err.code) {
37 throw new Error(
38 `The specified connection string for your PostgreSQL database might have illegal characters. Please check that it only contains allowed characters [a-zA-Z0-9]${EOL}${err.message}`
39 )
40 }
41
42 if (DatabaseErrorCode.migrationMissing === err.code) {
43 throw new Error(
44 `Migrations missing. Please run 'medusa migrations run' and try again.`
45 )
46 }
47
48 throw err
49 }