@@ -12,6 +12,13 @@ const {
12
12
DB_USER
13
13
} = process . env
14
14
15
+ // Exit Codes
16
+ // 0 - Success
17
+ // 2 - Unexpected error
18
+ // 10 - Unexpected error on idle client
19
+ // 20 - Unexpected error on inserting data to the database
20
+ // 30 - Unexpected error on fetching data from the data source
21
+
15
22
async function main ( ) {
16
23
const config = {
17
24
user : DB_USER ,
@@ -25,7 +32,7 @@ async function main () {
25
32
pool . on ( 'error' , ( err , client ) => {
26
33
console . error ( err )
27
34
console . error ( '--- Unexpected error on idle client, exiting ---' )
28
- process . exit ( - 1 )
35
+ process . exit ( 10 )
29
36
} )
30
37
31
38
const client = await pool . connect ( )
@@ -53,7 +60,7 @@ async function main () {
53
60
} catch ( error ) {
54
61
console . error ( error )
55
62
console . error ( "--- Can't fetch countries, exiting ---" )
56
- process . exit ( - 3 )
63
+ process . exit ( 30 )
57
64
}
58
65
59
66
// fetch cities
@@ -62,8 +69,8 @@ async function main () {
62
69
cities = await getCities ( )
63
70
} catch ( error ) {
64
71
console . error ( error )
65
- console . error ( "--- Can\ 't fetch cities, exiting ---" )
66
- process . exit ( - 2 )
72
+ console . error ( "--- Can't fetch cities, exiting ---" )
73
+ process . exit ( 30 )
67
74
}
68
75
69
76
try {
@@ -87,7 +94,11 @@ async function main () {
87
94
return client . query ( text , [ index , index , name , code , region , subregion , lat , long ] )
88
95
} )
89
96
90
- await Promise . all ( countryPromises )
97
+ await Promise . all ( countryPromises ) . catch ( err => {
98
+ console . error ( err )
99
+ console . error ( "--- Can't insert countries, exiting ---" )
100
+ process . exit ( 20 )
101
+ } )
91
102
92
103
const query = {
93
104
name : 'fetch' ,
@@ -125,7 +136,8 @@ async function main () {
125
136
} )
126
137
. catch ( err => {
127
138
console . error ( err ) ;
128
- process . exit ( 1 )
139
+ console . error ( "--- Can't insert cities, exiting ---" )
140
+ process . exit ( 20 )
129
141
} )
130
142
. then ( _ => {
131
143
console . log ( '--- Countries and cities inserted successfully ---' ) ;
@@ -134,7 +146,7 @@ async function main () {
134
146
135
147
} catch ( error ) {
136
148
console . error ( error )
137
- console . error ( "--- Error while inserting countries and cities into db , exiting ---" )
149
+ console . error ( "--- Failed to init countries and cities, exiting ---" )
138
150
process . exit ( 2 )
139
151
}
140
152
}
0 commit comments