@@ -250,6 +250,21 @@ export default class Response<S = unknown> {
250250 return this . resource ;
251251 }
252252
253+ /**
254+ * Merges an exception object into this response
255+ */
256+ public fromException ( exception : Exception ) {
257+ //sets the status code and message
258+ this . statusCode ( exception . code || 400 ) ;
259+ //set the error message
260+ this . _error = exception . message || 'Unknown Error' ;
261+ //set the errors
262+ this . errors . set ( exception . errors ) ;
263+ //set the stack trace
264+ this . _stack = exception . trace ( ) ;
265+ return this ;
266+ }
267+
253268 /**
254269 * Merges a success response object into this response
255270 */
@@ -297,7 +312,7 @@ export default class Response<S = unknown> {
297312 */
298313 public redirect ( url : string , code = 302 , status ?: string ) {
299314 //sets the status code and message
300- this . setStatus ( code , status ) ;
315+ this . statusCode ( code , status ) ;
301316 //set the header location
302317 this . headers . set ( 'Location' , url ) ;
303318 return this ;
@@ -306,9 +321,9 @@ export default class Response<S = unknown> {
306321 /**
307322 * Sets the body with checks
308323 */
309- public setBody ( type : string , body : Body , code = 200 , status ?: string ) {
324+ public set ( type : string , body : Body , code = 200 , status ?: string ) {
310325 //sets the status code and message
311- this . setStatus ( code , status ) ;
326+ this . statusCode ( code , status ) ;
312327 //set the mimetype
313328 this . _mimetype = type ;
314329 //set the body
@@ -334,7 +349,7 @@ export default class Response<S = unknown> {
334349 error = error . error ;
335350 }
336351 //sets the status code and message
337- this . setStatus ( code , status ) ;
352+ this . statusCode ( code , status ) ;
338353 //set the error message
339354 this . _error = error ;
340355 this . _stack = stack && stack . length > 0 ? stack : undefined ;
@@ -346,45 +361,45 @@ export default class Response<S = unknown> {
346361 /**
347362 * Sets the body as HTML with checks
348363 */
349- public setHTML ( body : string , code = 200 , status ?: string ) {
350- return this . setBody ( 'text/html' , body , code , status ) ;
364+ public html ( body : string , code = 200 , status ?: string ) {
365+ return this . set ( 'text/html' , body , code , status ) ;
351366 }
352367
353368 /**
354369 * Sets the body as JSON with checks
355370 */
356- public setJSON ( body : string | NestedObject , code = 200 , status ?: string ) {
371+ public json ( body : string | NestedObject , code = 200 , status ?: string ) {
357372 if ( typeof body !== 'string' ) {
358373 body = JSON . stringify ( body , null , 2 ) ;
359374 }
360- return this . setBody ( 'application/json' , body , code , status ) ;
375+ return this . set ( 'application/json' , body , code , status ) ;
361376 }
362377
363378 /**
364379 * Sets the body as Object with checks
365380 */
366- public setResults ( body : NestedObject , code = 200 , status ?: string ) {
381+ public results ( body : NestedObject , code = 200 , status ?: string ) {
367382 this . _total = 1 ;
368- return this . setBody ( 'application/json' , body , code , status ) ;
383+ return this . set ( 'application/json' , body , code , status ) ;
369384 }
370385
371386 /**
372387 * Sets the body as Array with checks
373388 */
374- public setRows (
389+ public rows (
375390 body : NestedObject [ ] ,
376391 total = 0 ,
377392 code = 200 ,
378393 status ?: string
379394 ) {
380395 this . _total = total ;
381- return this . setBody ( 'application/json' , body , code , status ) ;
396+ return this . set ( 'application/json' , body , code , status ) ;
382397 }
383398
384399 /**
385400 * Sets the status code and message
386401 */
387- public setStatus ( code : number , message ?: string ) {
402+ public statusCode ( code : number , message ?: string ) {
388403 this . _code = code ;
389404 this . _status = message || Status . get ( code ) ?. status || '' ;
390405 return this ;
@@ -393,8 +408,8 @@ export default class Response<S = unknown> {
393408 /**
394409 * Sets the body as XML with checks
395410 */
396- public setXML ( body : string , code = 200 , status ?: string ) {
397- return this . setBody ( 'text/xml' , body , code , status ) ;
411+ public xml ( body : string , code = 200 , status ?: string ) {
412+ return this . set ( 'text/xml' , body , code , status ) ;
398413 }
399414
400415 /**
0 commit comments