mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-11-22 14:23:57 +00:00
BETTERZON-38: Added error handler, preparation for DB accessing
This commit is contained in:
parent
5b04b810dc
commit
6b8acdab46
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,7 +1,7 @@
|
||||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||||
|
|
||||||
# compiled output
|
# compiled output
|
||||||
/dist
|
**/dist
|
||||||
/tmp
|
/tmp
|
||||||
/out-tsc
|
/out-tsc
|
||||||
# Only exists if Bazel was run
|
# Only exists if Bazel was run
|
||||||
|
|
11
Backend/Backend.iml
Normal file
11
Backend/Backend.iml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
1693
Backend/package-lock.json
generated
1693
Backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -14,7 +14,9 @@
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"helmet": "^4.2.0"
|
"helmet": "^4.2.0",
|
||||||
|
"mariadb": "^2.5.1",
|
||||||
|
"typeorm": "^0.2.29"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/cors": "^2.8.8",
|
"@types/cors": "^2.8.8",
|
||||||
|
|
13
Backend/src/common/http-exception.ts
Normal file
13
Backend/src/common/http-exception.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
export default class HttpException extends Error {
|
||||||
|
statusCode: number;
|
||||||
|
message: string;
|
||||||
|
error: string | null;
|
||||||
|
|
||||||
|
constructor(statusCode: number, message: string, error?: string) {
|
||||||
|
super(message);
|
||||||
|
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.message = message;
|
||||||
|
this.error = error || null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ import express from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import { productsRouter } from "./products/products.router";
|
import { productsRouter } from "./products/products.router";
|
||||||
|
import { errorHandler } from "./middleware/error.middleware";
|
||||||
|
import {notFoundHandler} from "./middleware/notFound.middleware";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
@ -33,6 +35,9 @@ app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use("/products", productsRouter);
|
app.use("/products", productsRouter);
|
||||||
|
|
||||||
|
app.use(errorHandler);
|
||||||
|
app.use(notFoundHandler);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Server Activation
|
* Server Activation
|
||||||
|
|
15
Backend/src/middleware/error.middleware.ts
Normal file
15
Backend/src/middleware/error.middleware.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import HttpException from "../common/http-exception";
|
||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
|
export const errorHandler = (
|
||||||
|
error: HttpException,
|
||||||
|
request: Request,
|
||||||
|
response: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
const status = error.statusCode || 500;
|
||||||
|
const message =
|
||||||
|
error.message || "It's not you. It's us. We are having some problems.";
|
||||||
|
|
||||||
|
response.status(status).send(message);
|
||||||
|
};
|
12
Backend/src/middleware/notFound.middleware.ts
Normal file
12
Backend/src/middleware/notFound.middleware.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
|
export const notFoundHandler = (
|
||||||
|
request: Request,
|
||||||
|
response: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
|
||||||
|
const message = "Resource not found";
|
||||||
|
|
||||||
|
response.status(404).send(message);
|
||||||
|
};
|
|
@ -3,8 +3,8 @@
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/e2e" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/e2e" isTestSource="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||||
|
|
Loading…
Reference in New Issue
Block a user