Skip to content

Commit a9bd712

Browse files
author
Pratik Das
committed
Merge branch 'node-errorhandling' of https://github.com/pratikdas/code-examples into node-errorhandling
2 parents e1dae42 + 0414422 commit a9bd712

File tree

74 files changed

+5123
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+5123
-0
lines changed

build-all.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ fi
100100

101101
if [[ "$MODULE" == "module6" ]]
102102
then
103+
build_maven_module "spring-boot/spring-boot-app-info"
103104
build_maven_module "spring-boot/spring-boot-null-safe-annotations"
104105
build maven_module "aws/cdkv2"
105106
build_maven_module "immutables"
@@ -132,6 +133,7 @@ fi
132133

133134
if [[ "$MODULE" == "module5" ]]
134135
then
136+
build_maven_module "spring-boot/beginners-guide"
135137
build_maven_module "aws/aws-dynamodb"
136138
build_maven_module "spring-boot/spring-boot-testconfiguration"
137139
build_maven_module "aws/springcloudrds"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.reflectoring;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface CSV {
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.reflectoring;
2+
3+
public class TestMarkerAnnotation {
4+
5+
public static void main(String[] args) {
6+
7+
XYZClient client = new XYZClient();
8+
Class clientClass = client.getClass();
9+
10+
if (clientClass.isAnnotationPresent(CSV.class)){
11+
System.out.println("Write client data to CSV.");
12+
} else {
13+
System.out.println("Write client data to Excel file.");
14+
}
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.reflectoring;
2+
3+
@CSV
4+
public class XYZClient {
5+
6+
}

nodejs/middleware/app.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import express, { Request, Response, NextFunction } from 'express'
2+
import morgan from 'morgan'
3+
4+
const app = express()
5+
const port:number = 3000
6+
7+
8+
interface Product {
9+
10+
name: string
11+
price: number
12+
brand: string
13+
category?: string
14+
}
15+
16+
interface ProductCreationResponse {
17+
productID: string
18+
result: string
19+
}
20+
21+
22+
class AppError extends Error{
23+
statusCode: number;
24+
25+
constructor(statusCode: number, message: string) {
26+
super(message);
27+
28+
Object.setPrototypeOf(this, new.target.prototype);
29+
this.name = Error.name;
30+
this.statusCode = statusCode;
31+
Error.captureStackTrace(this);
32+
}
33+
}
34+
35+
const requestLogger = (request: Request, response: Response, next: NextFunction) => {
36+
console.log(`${request.method} url:: ${request.url}`);
37+
next()
38+
}
39+
40+
app.use(express.static('images'))
41+
app.use(express.static('htmls'))
42+
app.use(requestLogger)
43+
44+
app.use(morgan('tiny'))
45+
46+
app.use('/products', express.json({ limit: 100 }))
47+
48+
// Error handling Middleware functions
49+
const errorLogger = (error: Error, request: Request, response: Response, next: NextFunction) => {
50+
console.log( `error ${error.message}`)
51+
next(error) // calling next middleware
52+
}
53+
54+
const errorResponder = (error: AppError, request: Request, response: Response, next: NextFunction) => {
55+
response.header("Content-Type", 'application/json')
56+
57+
const status = error.statusCode || 400
58+
response.status(status).send(error.message)
59+
}
60+
const invalidPathHandler = (request: Request, response: Response, next: NextFunction) => {
61+
response.status(400)
62+
response.send('invalid path')
63+
}
64+
65+
66+
67+
app.get('product', (request: Request, response: Response)=>{
68+
response.sendFile("productsample.html")
69+
})
70+
71+
// handle get request for path /
72+
app.get('/', (request: Request, response: Response) => {
73+
response.send('response for GET request');
74+
})
75+
76+
77+
const requireJsonContent = (request: Request, response: Response, next: NextFunction) => {
78+
if (request.headers['content-type'] !== 'application/json') {
79+
response.status(400).send('Server requires application/json')
80+
} else {
81+
next()
82+
}
83+
}
84+
85+
86+
const addProducts = (request: Request, response: Response, next: NextFunction) => {
87+
let products: Product[] = []
88+
89+
const name: string = request.body.name
90+
91+
const brand: string = request.body.brand
92+
93+
const category: string = request.body.category
94+
95+
console.log(name + " " + brand)
96+
97+
products.push({name: request.body.name, brand: request.body.brand, price: request.body.price})
98+
99+
const productCreationResponse: ProductCreationResponse = {productID: "12345", result: "success"}
100+
response.json(productCreationResponse)
101+
102+
response.status(200).json(products);
103+
}
104+
app.post('/products', addProducts)
105+
106+
app.get('/productswitherror', (request: Request, response: Response) => {
107+
let error: AppError = new AppError(400, `processing error in request at ${request.url}`)
108+
109+
throw error
110+
})
111+
112+
app.use(errorLogger)
113+
app.use(errorResponder)
114+
app.use(invalidPathHandler)
115+
116+
app.listen(port, () => {
117+
console.log(`Server listening at port ${port}.`)
118+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<h2>My sample product page</h2>
4+
<img src="sample.jpg" alt="sample"></img>
5+
</body>
6+
</html>
7.33 KB
Loading

nodejs/middleware/index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const express = require('express')
2+
const morgan = require('morgan')
3+
4+
const app = express();
5+
6+
7+
const requestLogger = (request, response, next) => {
8+
console.log(`${request.method} url:: ${request.url}`);
9+
next()
10+
}
11+
12+
app.use(express.static('images'))
13+
app.use(express.static('htmls'))
14+
15+
app.use(requestLogger)
16+
17+
app.use(morgan('tiny'))
18+
19+
app.use('/products', express.json({ limit: 100 }))
20+
21+
// Error handling Middleware functions
22+
const errorLogger = (error, request, response, next) => {
23+
console.log( `error ${error.message}`)
24+
next(error) // calling next middleware
25+
}
26+
27+
const errorResponder = (error, request, response, next) => {
28+
response.header("Content-Type", 'application/json')
29+
30+
const status = error.status || 400
31+
response.status(status).send(error.message)
32+
}
33+
const invalidPathHandler = (request, response, next) => {
34+
response.status(400)
35+
response.send('invalid path')
36+
}
37+
38+
39+
40+
app.get('product', (request, response)=>{
41+
response.sendFile("productsample.html")
42+
})
43+
44+
// handle get request for path /
45+
app.get('/', (request, response) => {
46+
response.send('response for GET request');
47+
})
48+
49+
50+
const requireJsonContent = (request, response, next) => {
51+
if (request.headers['content-type'] !== 'application/json') {
52+
response.status(400).send('Server requires application/json')
53+
} else {
54+
next()
55+
}
56+
}
57+
58+
// handle post request for path /products
59+
app.post('/products', requireJsonContent, (request, response) => {
60+
const products = []
61+
62+
const name = request.body.name
63+
64+
const brand = request.body.brand
65+
66+
const category = request.body.category
67+
68+
console.log(name + " " + brand)
69+
70+
products.push({name: request.body.name, brand: request.body.brand, price: request.body.price})
71+
72+
const productCreationResponse = {productID: "12345", result: "success"}
73+
response.json(productCreationResponse)
74+
})
75+
76+
app.get('/productswitherror', (request, response) => {
77+
let error = new Error(`processing error in request at ${request.url}`)
78+
error.statusCode = 400
79+
throw error
80+
})
81+
82+
app.use(errorLogger)
83+
app.use(errorResponder)
84+
app.use(invalidPathHandler)
85+
86+
const port = 3000
87+
app.listen(3000,
88+
() => console.log(`Server listening on port ${port}.`));
89+

0 commit comments

Comments
 (0)