Skip to content

Commit bf05152

Browse files
authored
feat(HyperRequest): Add throw on error flag
Throw on error is passed along to the `cfhttp` request. Additionally, two helper methods are defined: 1. `throwErrors` — sets the `throwOnError` flag to `true` 1. `ignoreErrors` — sets the `throwOnError` flag to `false` The default value of the flag is `false` to maintain backward compatibility.
1 parent 6a35d41 commit bf05152

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,31 @@ Check if the request has a query parameter with the given name.
422422
| ---- | ------ | -------- | ------- | ----------------------------------------- |
423423
| name | string | true | | The name of the query parameter to check. |
424424

425+
##### `setThrowOnError`
426+
427+
Sets the throw on error property for the request. If true, error codes and status
428+
will be turned in to exceptions.
429+
430+
| Name | Type | Required | Default | Description |
431+
| ---- | ------ | -------- | ------- | ----------------------------------------- |
432+
| value | boolean | true | | The value of the throw on error flag. |
433+
434+
##### `throwErrors`
435+
436+
A convenience method to throw on errors.
437+
438+
| Name | Type | Required | Default | Description |
439+
| ------------ | ---- | -------- | ------- | ----------- |
440+
| No arguments | | | | |
441+
442+
##### `allowErrors`
443+
444+
A convenience method to not throw on errors.
445+
446+
| Name | Type | Required | Default | Description |
447+
| ------------ | ---- | -------- | ------- | ----------- |
448+
| No arguments | | | | |
449+
425450
##### `setProperties`
426451

427452
Quickly set many request properties using a struct. The key should be the name

models/HyperRequest.cfc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ component accessors="true" {
7171
*/
7272
property name="queryParams";
7373

74+
/**
75+
* Flag to throw on a cfhttp error.
76+
*/
77+
property name="throwOnError" default="false";
78+
7479
/**
7580
* Initialize a new HyperRequest.
7681
*
@@ -332,6 +337,26 @@ component accessors="true" {
332337
return this;
333338
}
334339

340+
/**
341+
* A convenience method to not throw on errors.
342+
*
343+
* @returns The HyperRequest instance.
344+
*/
345+
function allowErrors() {
346+
setThrowOnError( false );
347+
return this;
348+
}
349+
350+
/**
351+
* A convenience method to throw on errors.
352+
*
353+
* @returns The HyperRequest instance.
354+
*/
355+
function throwErrors() {
356+
setThrowOnError( true );
357+
return this;
358+
}
359+
335360
/**
336361
* A convenience method to set the body format and Content-Type to json.
337362
*
@@ -557,7 +582,8 @@ component accessors="true" {
557582
method = getMethod(),
558583
redirect = false,
559584
username = getUsername(),
560-
password = getPassword()
585+
password = getPassword(),
586+
throwonerror = getThrowOnError()
561587
) {
562588
for ( var name in variables.headers ) {
563589
cfhttpparam(

0 commit comments

Comments
 (0)