Skip to content

Commit c1ac3d4

Browse files
committed
Make reformat() apply to initial payload
1 parent 7113a3e commit c1ac3d4

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ internals.apply = function (boom, data, decorate, statusCode, headers, message)
164164
boom.message = `${message}: ${boom.message}`;
165165
}
166166

167-
const payload = new internals.PayloadObject(boom, numberCode, false);
168-
boom.output = new internals.BoomOutput(numberCode, payload, headers);
167+
boom.output = new internals.BoomOutput(numberCode, headers);
168+
boom.reformat();
169169
}
170170
};
171171

@@ -198,13 +198,12 @@ internals.PayloadObject = class {
198198
internals.BoomOutput = class {
199199

200200
statusCode;
201-
payload;
201+
payload = {};
202202
headers;
203203

204-
constructor(statusCode, payload, headers) {
204+
constructor(statusCode, headers) {
205205

206206
this.statusCode = statusCode;
207-
this.payload = payload;
208207
this.headers = headers ? Hoek.clone(headers, { prototype: false, symbols: false }) : {};
209208
}
210209
};

test/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,5 +1126,45 @@ describe('Boom', () => {
11261126

11271127
Object.defineProperty(new Boom.Boom('oops'), 'reformat', { value: true });
11281128
});
1129+
1130+
it('can be implemented by subclasses to apply custom formatting', () => {
1131+
1132+
class MyBoom extends Boom.Boom {
1133+
1134+
reformat(...args) {
1135+
1136+
super.reformat(...args);
1137+
1138+
this.output.payload.custom = true;
1139+
}
1140+
}
1141+
1142+
const err = new MyBoom('boom', { statusCode: 400 });
1143+
expect(err.output.statusCode).to.equal(400);
1144+
expect(err.output.payload.message).to.equal('boom');
1145+
expect(err.output.payload.custom).to.be.true();
1146+
1147+
err.output.statusCode = 500;
1148+
err.reformat();
1149+
expect(err.output.statusCode).to.equal(500);
1150+
expect(err.output.payload.message).to.equal('An internal server error occurred');
1151+
expect(err.output.payload.custom).to.be.true();
1152+
});
1153+
1154+
it('prototype can be changed to always debug', (flags) => {
1155+
1156+
const proto = Boom.Boom.prototype.reformat;
1157+
flags.onCleanup = () => {
1158+
1159+
Boom.Boom.prototype.reformat = proto;
1160+
};
1161+
1162+
Boom.Boom.prototype.reformat = function () {
1163+
1164+
return proto.call(this, true);
1165+
};
1166+
1167+
expect(Boom.internal('DEBUG').output.payload.message).to.equal('DEBUG');
1168+
});
11291169
});
11301170
});

0 commit comments

Comments
 (0)