Skip to content

Commit 106ab2e

Browse files
edit logged classes (#23)
1 parent f7a0ede commit 106ab2e

File tree

6 files changed

+125
-18
lines changed

6 files changed

+125
-18
lines changed

src/js/server/endpoint/LoggedEndpoint.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ module.exports = class LoggedEndpoint {
1818
async handle(request) {
1919
this.#logger.debug(`HttpEndpoint's handling [${request.route().method}] ${request.route().path}`);
2020

21-
return await this.#origin.handle(request);
21+
try {
22+
return await this.#origin.handle(request);
23+
24+
} catch (e) {
25+
this.#logger.error(`HttpEndpoint's handling [${request.route().method}] ${request.route().path} error: ${e.message}`, e);
26+
27+
throw e;
28+
}
2229
}
2330
}

src/js/server/request/LoggedInputRequest.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ module.exports = class LoggedInputRequest {
1616
async flush() {
1717
this.#logger.debug(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} ${JSON.stringify(this.#inputStream.headers)}`);
1818

19-
return new LoggedInputRequest(await this.#origin.flush(), this.#logger);
19+
try {
20+
return new LoggedInputRequest(await this.#origin.flush(), this.#logger);
21+
22+
} catch (e) {
23+
this.#logger.error(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} error: ${e.message}`, e);
24+
25+
throw e;
26+
}
2027
}
2128

2229
route() {

src/js/server/response/LoggedOutputResponse.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ module.exports = class LoggedOutputResponse {
1616
}
1717

1818
flush() {
19-
const outputStream = this.#origin.flush();
19+
const outputStream = this.#loggedFlush();
2020

2121
this.#logger.debug(`HttpResponse: [${outputStream.req.method}] ${outputStream.req.url} - ${outputStream.statusCode}`);
2222

2323
return outputStream;
2424
}
25+
26+
#loggedFlush() {
27+
try {
28+
return this.#origin.flush();
29+
30+
} catch (e) {
31+
this.#logger.error(`HttpResponse error: ${e.message}`, e);
32+
33+
throw e;
34+
}
35+
}
2536
}

src/test/js/server/endpont/LoggedEndpoint.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const diagnosticOrigin = {
2020

2121
const diagnosticLogger = {
2222
debug() {
23+
},
24+
error() {
2325
}
2426
};
2527

@@ -47,11 +49,15 @@ function prepareDiagnostic() {
4749
mock.method(diagnosticOrigin, 'route');
4850
mock.method(diagnosticOrigin, 'handle');
4951

50-
diagnosticLogger.message = null;
52+
diagnosticLogger.options = {};
5153
diagnosticLogger.debug = (message) => {
52-
diagnosticLogger.message = message;
54+
diagnosticLogger.options.message = message;
55+
};
56+
diagnosticLogger.error = (message) => {
57+
diagnosticLogger.options.error = message;
5358
};
5459
mock.method(diagnosticLogger, 'debug');
60+
mock.method(diagnosticLogger, 'error');
5561

5662
mock.method(diagnosticRequest, 'route');
5763
}
@@ -186,6 +192,21 @@ describe('LoggedEndpoint', () => {
186192
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
187193
});
188194

195+
it('should call error of logger', async () => {
196+
diagnosticOrigin.handle = () => {throw new Error('handle error')};
197+
mock.method(diagnosticOrigin, 'handle');
198+
199+
await assert.rejects(() => new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest));
200+
201+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
202+
});
203+
204+
it('should not call error of logger', async () => {
205+
await new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest);
206+
207+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
208+
});
209+
189210
it('should call handle of origin', async () => {
190211
await new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest);
191212

@@ -219,6 +240,8 @@ describe('LoggedEndpoint', () => {
219240

220241
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
221242
assert.strictEqual(diagnosticOrigin.handle.mock.calls.length, 0);
243+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
244+
assert.strictEqual(diagnosticLogger.options.error.includes(`HttpEndpoint's handling [${testRoute.method}] ${testRoute.path} error:`), true);
222245
});
223246

224247
it('should fall when call handle of origin, cause error', async () => {
@@ -232,12 +255,14 @@ describe('LoggedEndpoint', () => {
232255

233256
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
234257
assert.strictEqual(diagnosticOrigin.handle.mock.calls.length, 1);
258+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
259+
assert.strictEqual(diagnosticLogger.options.error, `HttpEndpoint's handling [${testRoute.method}] ${testRoute.path} error: handle error`);
235260
});
236261

237262
it('should log correct message', async () => {
238263
await new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest);
239264

240-
assert.strictEqual(diagnosticLogger.message, `HttpEndpoint's handling [${testRoute.method}] ${testRoute.path}`);
265+
assert.strictEqual(diagnosticLogger.options.message, `HttpEndpoint's handling [${testRoute.method}] ${testRoute.path}`);
241266
});
242267

243268
it('should return same response object', async () => {

src/test/js/server/request/LoggedInputRequest.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const diagnosticOrigin = {
2121

2222
const diagnosticLogger = {
2323
debug() {
24+
},
25+
error() {
2426
}
2527
};
2628

@@ -54,11 +56,15 @@ function prepareDiagnostic() {
5456
mock.method(diagnosticOrigin, 'body');
5557
mock.method(diagnosticOrigin, 'headers');
5658

57-
diagnosticLogger.message = null;
59+
diagnosticLogger.options = {};
5860
diagnosticLogger.debug = (message) => {
59-
diagnosticLogger.message = message;
61+
diagnosticLogger.options.message = message;
62+
};
63+
diagnosticLogger.error = (message) => {
64+
diagnosticLogger.options.error = message;
6065
};
6166
mock.method(diagnosticLogger, 'debug');
67+
mock.method(diagnosticLogger, 'error');
6268
}
6369

6470
function resetDiagnostic() {
@@ -144,18 +150,34 @@ describe('LoggedInputRequest', () => {
144150
beforeEach(prepareDiagnostic);
145151
afterEach(resetDiagnostic);
146152

153+
it('should call flush of origin', async () => {
154+
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();
155+
156+
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
157+
});
158+
147159
it('should call debug of logger', async () => {
148160
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();
149161

150162
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
151163
});
152164

153-
it('should call flush of origin', async () => {
165+
it('should call error of logger', async () => {
166+
diagnosticOrigin.flush = () => {throw new Error('flush error')};
167+
mock.method(diagnosticOrigin, 'flush');
168+
169+
await assert.rejects(() => new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush());
170+
171+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
172+
});
173+
174+
it('should not call error of logger', async () => {
154175
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();
155176

156-
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
177+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
157178
});
158179

180+
159181
it('should fall when call debug of logger, cause null', async () => {
160182
await assert.rejects(() => new LoggedInputRequest(null, null, {}).flush(),
161183
{name: 'TypeError'});
@@ -178,11 +200,15 @@ describe('LoggedInputRequest', () => {
178200
});
179201

180202
it('should fall when call flush of origin, cause null', async () => {
181-
await assert.rejects(() => new LoggedInputRequest(null, diagnosticLogger, {}).flush(),
203+
await assert.rejects(() => new LoggedInputRequest(
204+
null, diagnosticLogger, {method: 'method', url: 'url'}
205+
).flush(),
182206
{name: 'TypeError'});
183207

184208
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
185209
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 0);
210+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
211+
assert.strictEqual(diagnosticLogger.options.error.includes('HttpRequest: [method] url error:'), true)
186212
});
187213

188214
it('should fall when call flush of origin, cause error', async () => {
@@ -191,11 +217,15 @@ describe('LoggedInputRequest', () => {
191217
};
192218
mock.method(diagnosticOrigin, 'flush');
193219

194-
await assert.rejects(() => new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush(),
220+
await assert.rejects(() => new LoggedInputRequest(
221+
diagnosticOrigin, diagnosticLogger, {method: 'method', url: 'url'}
222+
).flush(),
195223
{message: 'flush error'});
196224

197225
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
198226
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
227+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
228+
assert.strictEqual(diagnosticLogger.options.error.includes('HttpRequest: [method] url error: flush error'), true)
199229
});
200230

201231
it('should log correct message', async () => {
@@ -205,7 +235,7 @@ describe('LoggedInputRequest', () => {
205235
headers: {header: 'header'}
206236
}).flush();
207237

208-
assert.strictEqual(diagnosticLogger.message, 'HttpRequest: [method] url {"header":"header"}');
238+
assert.strictEqual(diagnosticLogger.options.message, 'HttpRequest: [method] url {"header":"header"}');
209239
});
210240

211241
it('should return another LoggedInputRequest', async () => {

src/test/js/server/response/LoggedOutputResponse.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const diagnosticOrigin = {
1515

1616
const diagnosticLogger = {
1717
debug() {
18+
},
19+
error() {
1820
}
1921
};
2022

@@ -37,11 +39,15 @@ function prepareDiagnostic() {
3739
mock.method(diagnosticOrigin, 'update');
3840
mock.method(diagnosticOrigin, 'flush');
3941

40-
diagnosticLogger.message = null;
42+
diagnosticLogger.options = {};
4143
diagnosticLogger.debug = (message) => {
42-
diagnosticLogger.message = message;
44+
diagnosticLogger.options.message = message;
45+
};
46+
diagnosticLogger.error = (message) => {
47+
diagnosticLogger.options.error = message;
4348
};
4449
mock.method(diagnosticLogger, 'debug');
50+
mock.method(diagnosticLogger, 'error');
4551
}
4652

4753
function resetDiagnostic() {
@@ -141,11 +147,29 @@ describe('LoggedOutputResponse', () => {
141147
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
142148
});
143149

150+
it('should call error of logger', () => {
151+
diagnosticOrigin.flush = () => {throw new Error('flush error')};
152+
mock.method(diagnosticOrigin, 'flush');
153+
154+
assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush());
155+
156+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
157+
});
158+
159+
it('should not call error of logger', async () => {
160+
await new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush();
161+
162+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
163+
});
164+
144165
it('should fall when call flush of origin, cause null', () => {
145-
assert.throws(() => new LoggedOutputResponse(null, diagnosticLogger).flush(), {name: 'TypeError'});
166+
assert.throws(() => new LoggedOutputResponse(null, diagnosticLogger).flush(),
167+
{name: 'TypeError'});
146168

147169
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 0);
148170
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 0);
171+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
172+
assert.strictEqual(diagnosticLogger.options.error.includes(`HttpResponse error:`), true);
149173
});
150174

151175
it('should fall when call flush of origin, cause error', () => {
@@ -154,16 +178,19 @@ describe('LoggedOutputResponse', () => {
154178
};
155179
mock.method(diagnosticOrigin, 'flush');
156180

157-
assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush(), {message: 'flush error'});
181+
assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush(),
182+
{message: 'flush error'});
158183

159184
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
160185
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 0);
186+
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
187+
assert.strictEqual(diagnosticLogger.options.error.includes(`HttpResponse error: flush error`), true);
161188
});
162189

163190
it('should log correct message', () => {
164191
new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush();
165192

166-
assert.strictEqual(diagnosticLogger.message, 'HttpResponse: [method] url - statusCode');
193+
assert.strictEqual(diagnosticLogger.options.message, 'HttpResponse: [method] url - statusCode');
167194
});
168195

169196
it('should return same outputStream', () => {

0 commit comments

Comments
 (0)