Skip to content

Commit 1fd8cbe

Browse files
refactoring exception handling in InputRequest.js, InputResponse.js and OutputResponse.js
1 parent 4c59856 commit 1fd8cbe

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

src/js/bun/server/response/OutputResponse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = class OutputResponse {
2323
});
2424

2525
} catch (e) {
26-
throw new Error(e.message, {cause: 'INVALID_RESPONSE'})
26+
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
2727
}
2828
}
2929

src/js/client/response/InputResponse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = class InputResponse {
1212
}
1313

1414
flush() {
15-
try {
16-
return new Promise((resolve, reject) => {
15+
return new Promise((resolve, reject) => {
16+
try {
1717
this.#inputStream.once('error', (e) => reject(new Error(e.message, {cause: 'INVALID_RESPONSE'})));
1818

1919
let chunks = [];
@@ -25,11 +25,11 @@ module.exports = class InputResponse {
2525
body: Buffer.concat(chunks)
2626
}
2727
)));
28-
});
2928

30-
} catch (e) {
31-
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
32-
}
29+
} catch (e) {
30+
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
31+
}
32+
});
3333
}
3434

3535
statusCode() {

src/js/server/request/InputRequest.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,27 @@ module.exports = class InputRequest {
1313

1414
flush() {
1515
return new Promise((resolve, reject) => {
16-
this.#inputStream.once('error', (e) =>
17-
reject(new Error(e.message, {cause: 'INVALID_REQUEST'}))
18-
);
19-
20-
let chunks = [];
21-
this.#inputStream.on('data', (chunk) => chunks.push(chunk));
22-
this.#inputStream.on('end', () => resolve(new InputRequest(
23-
this.#inputStream,
24-
{
25-
method: this.#inputStream.method,
26-
path: new URL(this.#inputStream.url, 'http://dummy').pathname,
27-
query: new URL(this.#inputStream.url, 'http://dummy').searchParams,
28-
headers: new Headers(this.#inputStream.headers),
29-
body: Buffer.concat(chunks)
30-
}
31-
)));
16+
try {
17+
this.#inputStream.once('error', (e) =>
18+
reject(new Error(e.message, {cause: 'INVALID_REQUEST'}))
19+
);
20+
21+
let chunks = [];
22+
this.#inputStream.on('data', (chunk) => chunks.push(chunk));
23+
this.#inputStream.on('end', () => resolve(new InputRequest(
24+
this.#inputStream,
25+
{
26+
method: this.#inputStream.method,
27+
path: new URL(this.#inputStream.url, 'http://dummy').pathname,
28+
query: new URL(this.#inputStream.url, 'http://dummy').searchParams,
29+
headers: new Headers(this.#inputStream.headers),
30+
body: Buffer.concat(chunks)
31+
}
32+
)));
33+
34+
} catch (e) {
35+
throw new Error(e.message, {cause: 'INVALID_REQUEST'});
36+
}
3237
});
3338
}
3439

src/test/js/bun/server/response/OutputResponse.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('OutputResponse', () => {
6060
assert.doesNotThrow(() => {
6161
new OutputResponse();
6262
new OutputResponse(testOptions);
63+
new OutputResponse(testOptions, {});
6364
});
6465
});
6566
});
@@ -111,7 +112,7 @@ describe('OutputResponse', () => {
111112
diagnosticOptions.spy.diagnosticStatusCode = spyOn(diagnosticOptions, 'diagnosticStatusCode');
112113

113114
assert.throws(() => new OutputResponse(diagnosticOptions).flush(),
114-
{message: 'statusCode error'});
115+
{message: 'statusCode error', cause: 'INVALID_RESPONSE'});
115116

116117
expect(diagnosticOptions.spy.diagnosticBody).toHaveBeenCalledTimes(0);
117118
expect(diagnosticOptions.spy.diagnosticStatusCode).toHaveBeenCalledTimes(1);
@@ -125,7 +126,7 @@ describe('OutputResponse', () => {
125126
diagnosticOptions.spy.diagnosticHeaders = spyOn(diagnosticOptions, 'diagnosticHeaders');
126127

127128
assert.throws(() => new OutputResponse(diagnosticOptions).flush(),
128-
{message: 'headers error'});
129+
{message: 'headers error', cause: 'INVALID_RESPONSE'});
129130

130131
expect(diagnosticOptions.spy.diagnosticBody).toHaveBeenCalledTimes(0);
131132
expect(diagnosticOptions.spy.diagnosticStatusCode).toHaveBeenCalledTimes(1);
@@ -139,7 +140,7 @@ describe('OutputResponse', () => {
139140
diagnosticOptions.spy.diagnosticBody = spyOn(diagnosticOptions, 'diagnosticBody');
140141

141142
assert.throws(() => new OutputResponse(diagnosticOptions).flush(),
142-
{message: 'body error'});
143+
{message: 'body error', cause: 'INVALID_RESPONSE'});
143144

144145
expect(diagnosticOptions.spy.diagnosticBody).toHaveBeenCalledTimes(1);
145146
expect(diagnosticOptions.spy.diagnosticStatusCode).toHaveBeenCalledTimes(1);
@@ -162,6 +163,17 @@ describe('OutputResponse', () => {
162163
}));
163164
});
164165

166+
test('should not be updated', () => {
167+
const resultedOutputStream = new OutputResponse(testOptions)
168+
.update().flush();
169+
170+
assert.deepStrictEqual(resultedOutputStream,
171+
new Response(testOptions.body, {
172+
status: testOptions.statusCode,
173+
headers: testOptions.headers
174+
}));
175+
});
176+
165177
test('should be updated by statusCode, body and headers', () => {
166178
const resultedOutputStream = new OutputResponse()
167179
.update(testOptions).flush();

src/test/js/client/response/InputResponse.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ describe('InputResponse', () => {
123123
describe('flush', () => {
124124

125125
it('should fall on inputStream, cause null', async () => {
126-
await assert.rejects(() => new InputResponse().flush(), {name: 'TypeError'});
126+
await assert.rejects(() => new InputResponse().flush(),
127+
{cause: 'INVALID_RESPONSE'});
127128

128129
assert.strictEqual(diagnosticInputStream.once.mock.calls.length, 0);
129130
assert.strictEqual(diagnosticInputStream.on.mock.calls.length, 0);

src/test/js/server/request/InputRequest.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ describe('InputRequest', () => {
156156
afterEach(resetDiagnostic);
157157

158158
it('should fall on inputStream, cause null', async () => {
159-
await assert.rejects(() => new InputRequest().flush(), {name: 'TypeError'});
159+
await assert.rejects(() => new InputRequest().flush(),
160+
{cause: 'INVALID_REQUEST'});
160161

161162
assert.strictEqual(diagnosticInputStream.once.mock.calls.length, 0);
162163
assert.strictEqual(diagnosticInputStream.on.mock.calls.length, 0);

0 commit comments

Comments
 (0)