Skip to content

Commit 6b5498b

Browse files
committed
update exit codes
1 parent 032bfcd commit 6b5498b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

scripts/init-countries.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ const {
1212
DB_USER
1313
} = process.env
1414

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+
1522
async function main () {
1623
const config = {
1724
user: DB_USER,
@@ -25,7 +32,7 @@ async function main () {
2532
pool.on('error', (err, client) => {
2633
console.error(err)
2734
console.error('--- Unexpected error on idle client, exiting ---')
28-
process.exit(-1)
35+
process.exit(10)
2936
})
3037

3138
const client = await pool.connect()
@@ -53,7 +60,7 @@ async function main () {
5360
} catch (error) {
5461
console.error(error)
5562
console.error("--- Can't fetch countries, exiting ---")
56-
process.exit(-3)
63+
process.exit(30)
5764
}
5865

5966
// fetch cities
@@ -62,8 +69,8 @@ async function main () {
6269
cities = await getCities()
6370
} catch (error) {
6471
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)
6774
}
6875

6976
try {
@@ -87,7 +94,11 @@ async function main () {
8794
return client.query(text, [index, index, name, code, region, subregion, lat, long])
8895
})
8996

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+
})
91102

92103
const query = {
93104
name: 'fetch',
@@ -125,7 +136,8 @@ async function main () {
125136
})
126137
.catch(err => {
127138
console.error(err);
128-
process.exit(1)
139+
console.error("--- Can't insert cities, exiting ---")
140+
process.exit(20)
129141
})
130142
.then(_ => {
131143
console.log('--- Countries and cities inserted successfully ---');
@@ -134,7 +146,7 @@ async function main () {
134146

135147
} catch (error) {
136148
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 ---")
138150
process.exit(2)
139151
}
140152
}

0 commit comments

Comments
 (0)