Skip to content

Commit 6674912

Browse files
committed
Add try catch on all db operations to find out where things go wrong
1 parent 985f30c commit 6674912

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

app/db.js

+55-39
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,34 @@ export async function addCategory(category_name) {
4444
}
4545

4646
export function getIncs(numberOfDaysAgo) {
47-
if (numberOfDaysAgo === undefined) {
48-
const query = {
49-
text: `SELECT * FROM incs`,
50-
};
51-
return client.query(query);
52-
} else {
53-
// Calculate the date range
54-
const dateAgo = new Date();
55-
dateAgo.setDate(dateAgo.getDate() - numberOfDaysAgo);
56-
const formattedDateAgo = dateAgo.toISOString(); // ISO8601 format
47+
try {
48+
if (numberOfDaysAgo === undefined) {
49+
const query = {
50+
text: `SELECT * FROM incs`,
51+
};
52+
return client.query(query);
53+
} else {
54+
// Calculate the date range
55+
const dateAgo = new Date();
56+
dateAgo.setDate(dateAgo.getDate() - numberOfDaysAgo);
57+
const formattedDateAgo = dateAgo.toISOString(); // ISO8601 format
5758

58-
// Construct the SQL query with parameterized query
59-
const query = {
60-
text: `SELECT * FROM incs WHERE created_at >= $1`,
61-
values: [formattedDateAgo],
62-
};
63-
return client.query(query);
59+
// Construct the SQL query with parameterized query
60+
const query = {
61+
text: `SELECT * FROM incs WHERE created_at >= $1`,
62+
values: [formattedDateAgo],
63+
};
64+
return client.query(query);
65+
}
66+
}
67+
catch (err) {
68+
console.error("Error fetching incs", err
69+
);
6470
}
6571
}
6672

6773
export function getIncByCategory(numberOfDaysAgo) {
74+
try{
6875
if (numberOfDaysAgo === undefined) {
6976
const query = {
7077
text: `SELECT category, COUNT(*) FROM incs GROUP BY category`,
@@ -84,37 +91,46 @@ export function getIncByCategory(numberOfDaysAgo) {
8491
return client.query(query);
8592
}
8693
}
94+
catch (err) {
95+
console.error("Error fetching incs", err);
96+
}
97+
}
8798

8899
export async function getIncNumberByWeek(numberOfDaysAgo) {
89-
let queryText;
100+
try {
101+
let queryText;
90102

91-
if (numberOfDaysAgo === undefined) {
92-
queryText = `
93-
SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) AS count
94-
FROM incs
95-
GROUP BY week
96-
ORDER BY week;
97-
`;
98-
} else {
99-
// Calculate the date range
100-
const dateAgo = new Date();
101-
dateAgo.setDate(dateAgo.getDate() - numberOfDaysAgo);
102-
const formattedDateAgo = dateAgo.toISOString(); // ISO8601 format
103-
104-
// Construct the SQL query with parameterized query
105-
queryText = {
106-
text: `
103+
if (numberOfDaysAgo === undefined) {
104+
queryText = `
107105
SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) AS count
108106
FROM incs
109-
WHERE created_at >= $1
110107
GROUP BY week
111108
ORDER BY week;
112-
`,
113-
values: [formattedDateAgo],
114-
};
115-
}
109+
`;
110+
} else {
111+
// Calculate the date range
112+
const dateAgo = new Date();
113+
dateAgo.setDate(dateAgo.getDate() - numberOfDaysAgo);
114+
const formattedDateAgo = dateAgo.toISOString(); // ISO8601 format
115+
116+
// Construct the SQL query with parameterized query
117+
queryText = {
118+
text: `
119+
SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) AS count
120+
FROM incs
121+
WHERE created_at >= $1
122+
GROUP BY week
123+
ORDER BY week;
124+
`,
125+
values: [formattedDateAgo],
126+
};
127+
}
116128

117-
return await client.query(queryText);
129+
return await client.query(queryText);
130+
}
131+
catch (err) {
132+
console.error("Error fetching incs", err);
133+
}
118134
}
119135

120136
export async function getCategoriesArray() {

0 commit comments

Comments
 (0)