Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/js/server/endpoint/LoggedEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ module.exports = class LoggedEndpoint {
async handle(request) {
this.#logger.debug(`HttpEndpoint's handling [${request.route().method}] ${request.route().path}`);

return await this.#origin.handle(request);
try {
return await this.#origin.handle(request);

} catch (e) {
this.#logger.error(`HttpEndpoint's handling [${request.route().method}] ${request.route().path} error: ${e.message}`, e);

throw e;
}
}
}
9 changes: 8 additions & 1 deletion src/js/server/request/LoggedInputRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ module.exports = class LoggedInputRequest {
async flush() {
this.#logger.debug(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} ${JSON.stringify(this.#inputStream.headers)}`);

return new LoggedInputRequest(await this.#origin.flush(), this.#logger);
try {
return new LoggedInputRequest(await this.#origin.flush(), this.#logger);

} catch (e) {
this.#logger.error(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} error: ${e.message}`, e);

throw e;
}
}

route() {
Expand Down
13 changes: 12 additions & 1 deletion src/js/server/response/LoggedOutputResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ module.exports = class LoggedOutputResponse {
}

flush() {
const outputStream = this.#origin.flush();
const outputStream = this.#loggedFlush();

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

return outputStream;
}

#loggedFlush() {
try {
return this.#origin.flush();

} catch (e) {
this.#logger.error(`HttpResponse error: ${e.message}`, e);

throw e;
}
}
}
31 changes: 28 additions & 3 deletions src/test/js/server/endpont/LoggedEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const diagnosticOrigin = {

const diagnosticLogger = {
debug() {
},
error() {
}
};

Expand Down Expand Up @@ -47,11 +49,15 @@ function prepareDiagnostic() {
mock.method(diagnosticOrigin, 'route');
mock.method(diagnosticOrigin, 'handle');

diagnosticLogger.message = null;
diagnosticLogger.options = {};
diagnosticLogger.debug = (message) => {
diagnosticLogger.message = message;
diagnosticLogger.options.message = message;
};
diagnosticLogger.error = (message) => {
diagnosticLogger.options.error = message;
};
mock.method(diagnosticLogger, 'debug');
mock.method(diagnosticLogger, 'error');

mock.method(diagnosticRequest, 'route');
}
Expand Down Expand Up @@ -186,6 +192,21 @@ describe('LoggedEndpoint', () => {
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
});

it('should call error of logger', async () => {
diagnosticOrigin.handle = () => {throw new Error('handle error')};
mock.method(diagnosticOrigin, 'handle');

await assert.rejects(() => new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest));

assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
});

it('should not call error of logger', async () => {
await new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest);

assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
});

it('should call handle of origin', async () => {
await new LoggedEndpoint(diagnosticOrigin, diagnosticLogger).handle(diagnosticRequest);

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

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

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

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

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

assert.strictEqual(diagnosticLogger.message, `HttpEndpoint's handling [${testRoute.method}] ${testRoute.path}`);
assert.strictEqual(diagnosticLogger.options.message, `HttpEndpoint's handling [${testRoute.method}] ${testRoute.path}`);
});

it('should return same response object', async () => {
Expand Down
44 changes: 37 additions & 7 deletions src/test/js/server/request/LoggedInputRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const diagnosticOrigin = {

const diagnosticLogger = {
debug() {
},
error() {
}
};

Expand Down Expand Up @@ -54,11 +56,15 @@ function prepareDiagnostic() {
mock.method(diagnosticOrigin, 'body');
mock.method(diagnosticOrigin, 'headers');

diagnosticLogger.message = null;
diagnosticLogger.options = {};
diagnosticLogger.debug = (message) => {
diagnosticLogger.message = message;
diagnosticLogger.options.message = message;
};
diagnosticLogger.error = (message) => {
diagnosticLogger.options.error = message;
};
mock.method(diagnosticLogger, 'debug');
mock.method(diagnosticLogger, 'error');
}

function resetDiagnostic() {
Expand Down Expand Up @@ -144,18 +150,34 @@ describe('LoggedInputRequest', () => {
beforeEach(prepareDiagnostic);
afterEach(resetDiagnostic);

it('should call flush of origin', async () => {
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();

assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
});

it('should call debug of logger', async () => {
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();

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

it('should call flush of origin', async () => {
it('should call error of logger', async () => {
diagnosticOrigin.flush = () => {throw new Error('flush error')};
mock.method(diagnosticOrigin, 'flush');

await assert.rejects(() => new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush());

assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
});

it('should not call error of logger', async () => {
await new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush();

assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
});


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

it('should fall when call flush of origin, cause null', async () => {
await assert.rejects(() => new LoggedInputRequest(null, diagnosticLogger, {}).flush(),
await assert.rejects(() => new LoggedInputRequest(
null, diagnosticLogger, {method: 'method', url: 'url'}
).flush(),
{name: 'TypeError'});

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

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

await assert.rejects(() => new LoggedInputRequest(diagnosticOrigin, diagnosticLogger, {}).flush(),
await assert.rejects(() => new LoggedInputRequest(
diagnosticOrigin, diagnosticLogger, {method: 'method', url: 'url'}
).flush(),
{message: 'flush error'});

assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 1);
assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.options.error.includes('HttpRequest: [method] url error: flush error'), true)
});

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

assert.strictEqual(diagnosticLogger.message, 'HttpRequest: [method] url {"header":"header"}');
assert.strictEqual(diagnosticLogger.options.message, 'HttpRequest: [method] url {"header":"header"}');
});

it('should return another LoggedInputRequest', async () => {
Expand Down
37 changes: 32 additions & 5 deletions src/test/js/server/response/LoggedOutputResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const diagnosticOrigin = {

const diagnosticLogger = {
debug() {
},
error() {
}
};

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

diagnosticLogger.message = null;
diagnosticLogger.options = {};
diagnosticLogger.debug = (message) => {
diagnosticLogger.message = message;
diagnosticLogger.options.message = message;
};
diagnosticLogger.error = (message) => {
diagnosticLogger.options.error = message;
};
mock.method(diagnosticLogger, 'debug');
mock.method(diagnosticLogger, 'error');
}

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

it('should call error of logger', () => {
diagnosticOrigin.flush = () => {throw new Error('flush error')};
mock.method(diagnosticOrigin, 'flush');

assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush());

assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
});

it('should not call error of logger', async () => {
await new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush();

assert.strictEqual(diagnosticLogger.error.mock.calls.length, 0);
});

it('should fall when call flush of origin, cause null', () => {
assert.throws(() => new LoggedOutputResponse(null, diagnosticLogger).flush(), {name: 'TypeError'});
assert.throws(() => new LoggedOutputResponse(null, diagnosticLogger).flush(),
{name: 'TypeError'});

assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 0);
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 0);
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.options.error.includes(`HttpResponse error:`), true);
});

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

assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush(), {message: 'flush error'});
assert.throws(() => new LoggedOutputResponse(diagnosticOrigin, diagnosticLogger).flush(),
{message: 'flush error'});

assert.strictEqual(diagnosticOrigin.flush.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.debug.mock.calls.length, 0);
assert.strictEqual(diagnosticLogger.error.mock.calls.length, 1);
assert.strictEqual(diagnosticLogger.options.error.includes(`HttpResponse error: flush error`), true);
});

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

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

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