added error handling system in logger
This commit is contained in:
@@ -11,23 +11,37 @@ import { assignUserToStatus } from '../services/db/statusService';
|
|||||||
import { Request, response, Response } from 'express';
|
import { Request, response, Response } from 'express';
|
||||||
import { getUserRoles } from '../services/db/rolesService';
|
import { getUserRoles } from '../services/db/rolesService';
|
||||||
import { requireLogin, requireRole } from '../middleware/auth';
|
import { requireLogin, requireRole } from '../middleware/auth';
|
||||||
|
import { logger } from '../services/logging/logger';
|
||||||
|
|
||||||
//get CoC
|
//get CoC
|
||||||
router.get('/coc', async (req: Request, res: Response) => {
|
router.get('/coc', async (req: Request, res: Response) => {
|
||||||
const output = await fetch(`${process.env.DOC_HOST}/api/pages/714`, {
|
try {
|
||||||
headers: {
|
const response = await fetch(`${process.env.DOC_HOST}/api/pages/714`, {
|
||||||
Authorization: `Token ${process.env.DOC_TOKEN_ID}:${process.env.DOC_TOKEN_SECRET}`,
|
headers: {
|
||||||
}
|
Authorization: `Token ${process.env.DOC_TOKEN_ID}:${process.env.DOC_TOKEN_SECRET}`,
|
||||||
})
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (output.ok) {
|
if (!response.ok) {
|
||||||
const out = await output.json();
|
const text = await response.text();
|
||||||
|
logger.error('app', 'Failed to fetch LOA policy from Bookstack', {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
body: text,
|
||||||
|
});
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
const out = await response.json();
|
||||||
res.status(200).json(out.html);
|
res.status(200).json(out.html);
|
||||||
} else {
|
|
||||||
console.error("Failed to fetch LOA policy from bookstack");
|
} catch (error) {
|
||||||
|
logger.error('app', 'Error fetching LOA policy from Bookstack', {
|
||||||
|
error: error instanceof Error ? error.message : String(error),
|
||||||
|
});
|
||||||
res.sendStatus(500);
|
res.sendStatus(500);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
// POST /application
|
// POST /application
|
||||||
@@ -79,7 +93,7 @@ router.get('/me', [requireLogin], async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let application = await getMemberApplication(userID);
|
let application = await getMemberApplication(userID);
|
||||||
|
|
||||||
if (application === undefined) {
|
if (application === undefined) {
|
||||||
res.sendStatus(204);
|
res.sendStatus(204);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -29,7 +29,11 @@ function emitLog(header: LogHeader, payload: LogPayload = {}) {
|
|||||||
if (!shouldLog(header.depth)) return;
|
if (!shouldLog(header.depth)) return;
|
||||||
|
|
||||||
const logLine = { ...header, ...payload };
|
const logLine = { ...header, ...payload };
|
||||||
console.log(JSON.stringify(logLine));
|
|
||||||
|
if (header.level === 'error')
|
||||||
|
console.error(JSON.stringify(logLine))
|
||||||
|
else
|
||||||
|
console.log(JSON.stringify(logLine));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const logger = {
|
export const logger = {
|
||||||
|
|||||||
Reference in New Issue
Block a user