Skip to content

Commit

Permalink
[Changes] #7 Error Handling, to include basic custom Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Jan 22, 2024
1 parent a28f896 commit 00cc0ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { config } from 'dotenv';
import express from 'express';
import hpp from 'hpp';
import cache from './cache';
import * as error from "./error";
import writeRouter from '@src/controller/write';
import path from 'path';
import logger from '@src/scripts/logger';
Expand All @@ -18,12 +19,15 @@ app.use(cache);
app.get('/', (req, res) => {
res.send('Hello World, via TypeScript and Node.js!');
});

app.use('/write', writeRouter);

// use httpdocs as static folder
app.use('/', express.static(path.join(__dirname, 'httpdocs')))

// error handling
app.use(error.notFound);
app.use(error.handler);

// init server
app.listen(80, () => {
logger.log(`Server running //localhost:80`);
Expand Down
7 changes: 5 additions & 2 deletions src/scripts/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default {
log: (message:string|JSON) => {
fs.appendFileSync(logPath, `${date} \t|\t ${message} \n`);
console.log(message);

}
},
error: (message:string|JSON|Response.Error) => {
fs.appendFileSync(logPath, `${date} \t|\t ERROR: ${message} \n`);
console.error(message);
},
}
14 changes: 14 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@

/* eslint-disable @typescript-eslint/no-unused-vars */

type NumericRange<START extends number, END extends number, ARR extends unknown[] = [], ACC extends number = never> =
ARR['length'] extends END ? ACC | START | END :
NumericRange<START, END, [...ARR, 1], ARR[START] extends undefined ? ACC : ACC | ARR['length']>;

namespace Response {
interface Message {
message: string;
data?: string|JSON;
}

interface Error extends Response.Message {
stack?: string,
name?: string
}
}
namespace Models {
interface IEntry {
/**
Expand Down

0 comments on commit 00cc0ba

Please sign in to comment.