-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from JooLuiz/feat/load-page
[feat] - Implementing Error handler and loadFile function
- Loading branch information
Showing
19 changed files
with
274 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,7 @@ node_modules/ | |
.editorconfig | ||
.gitignore | ||
.npmrc | ||
|
||
src/static/ | ||
|
||
src/dev.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "simpler-server", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Simpler is a simple Node.js server, designed to rapidly provide a base to start your server projects.", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
|
@@ -16,7 +16,7 @@ | |
"type": "git", | ||
"url": "[email protected]:JooLuiz/simpler.git" | ||
}, | ||
"keywords": [], | ||
"keywords": ["Node Server"], | ||
"author": "João Luiz de Castro (https://github.com/JooLuiz)", | ||
"license": "ISC", | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface IErrorHandler { | ||
handleError: HandleErrorFunction; | ||
setCustomErrorHandler: SetCustomErrorHandlerFunction; | ||
} | ||
|
||
export default IErrorHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ServerResponse } from "http"; | ||
import type ILogger from "../Logger/logger.interface"; | ||
import type IErrorHandler from "./errorHandler.interface"; | ||
|
||
import Logger from "../Logger"; | ||
|
||
class ErrorHandler implements IErrorHandler { | ||
private logger: ILogger; | ||
|
||
constructor(isVerbose: boolean) { | ||
this.logger = new Logger(isVerbose); | ||
} | ||
|
||
private defaultErrorHandler: HandleErrorFunction = ( | ||
res: ServerResponse, | ||
error: Error | ||
) => { | ||
this.logger.error( | ||
`An error happening when processing the callback: ${error.message}` | ||
); | ||
res.writeHead(500, { "Content-Type": "application/json" }); | ||
res.end( | ||
JSON.stringify({ message: "Internal Server Error", error: error.message }) | ||
); | ||
}; | ||
|
||
private customErrorHandler?: HandleErrorFunction; | ||
|
||
public handleError(res: ServerResponse, error: Error): void { | ||
if (this.customErrorHandler) { | ||
this.customErrorHandler(res, error); | ||
} else { | ||
this.defaultErrorHandler(res, error); | ||
} | ||
} | ||
|
||
public setCustomErrorHandler(handler: HandleErrorFunction): void { | ||
this.customErrorHandler = handler; | ||
} | ||
} | ||
|
||
export default ErrorHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ErrorHandler from "./errorHandler"; | ||
|
||
export default ErrorHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
interface ILogger { | ||
log: (message: string) => void; | ||
logIfVerbose: (message: string, isVerbose: boolean) => void; | ||
error: (message: string) => void; | ||
log: LoggerFunction; | ||
logIfVerbose: LoggerFunction; | ||
error: LoggerFunction; | ||
} | ||
|
||
export default ILogger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.