Skip to content

Commit f14580b

Browse files
authored
Merge pull request #87 from TheDMSGroup/improvement/error-validation
Improvement to client error validation
2 parents 07b27cd + 007522a commit f14580b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Model/ApiPayloadOperation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ApiPayloadOperation
3737
/** @var array */
3838
protected $responseExpected;
3939

40-
/** @var array */
40+
/** @var string */
4141
protected $successDefinition;
4242

4343
/** @var array */
@@ -104,7 +104,7 @@ public function __construct(
104104
$this->name = !empty($operation->name) ? $operation->name : $this->id;
105105
$this->request = isset($operation->request) ? $operation->request : [];
106106
$this->responseExpected = isset($operation->response) ? $operation->response : [];
107-
$this->successDefinition = isset($operation->response->success->definition) ? $operation->response->success->definition : [];
107+
$this->successDefinition = isset($operation->response->success->definition) ? $operation->response->success->definition : null;
108108
$this->tokenHelper = $tokenHelper;
109109
$this->test = $test;
110110
$this->updatePayload = $updatePayload;

Model/ApiPayloadResponse.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ApiPayloadResponse
3636
/** @var bool */
3737
protected $test;
3838

39-
/** @var array */
39+
/** @var string */
4040
protected $successDefinition;
4141

4242
/** @var bool */
@@ -392,10 +392,14 @@ public function validate()
392392
);
393393
}
394394

395-
// Always consider a 500 to be an error.
396-
if (isset($this->responseActual['status']) && 500 == $this->responseActual['status']) {
395+
// Always consider a 5xx to be an error.
396+
if (
397+
is_int($this->responseActual['status'])
398+
&& $this->responseActual['status'] >= 500
399+
&& $this->responseActual['status'] < 600
400+
) {
397401
throw new ContactClientException(
398-
'Client responded with a 500 server error code.',
402+
'Client responded with a '.$this->responseActual['status'].' server error code.',
399403
0,
400404
null,
401405
Stat::TYPE_ERROR,
@@ -404,7 +408,7 @@ public function validate()
404408
}
405409

406410
// If there is no success definition, than do the default test of a 200 ok status check.
407-
if (!$this->successDefinition) {
411+
if (empty($this->successDefinition) || in_array($this->successDefinition, ['null', '[]'])) {
408412
if (!$this->responseActual['status'] || 200 != $this->responseActual['status']) {
409413
throw new ContactClientException(
410414
'Status code is not 200. Default validation failure.',
@@ -414,9 +418,7 @@ public function validate()
414418
true
415419
);
416420
}
417-
}
418-
419-
if (!empty($this->successDefinition) && 'null' !== $this->successDefinition) {
421+
} else {
420422
// Standard success definition validation.
421423
$filter = new FilterHelper();
422424
try {

0 commit comments

Comments
 (0)