Skip to content

Commit

Permalink
Merge pull request #77 from gearbox-solutions/log-query-even-on-errors
Browse files Browse the repository at this point in the history
Log API requests, even on errors
  • Loading branch information
Smef authored Aug 22, 2024
2 parents 49c9131 + ad856cd commit c2271d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"require-dev": {
"orchestra/testbench": "^9.0",
"mockery/mockery": "^1.5.1",
"laravel/pint": "^1.1"
"laravel/pint": "^1.17"
},
"extra": {
"laravel": {
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Eloquent/FMModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function createModelsFromRecordSet(BaseCollection $records): Colle
// return an empty Eloquent/Collection (or the custom collection specified in the model) if an empty collection was
// passed in.
if ($records->count() === 0) {
return (new static())->newCollection();
return (new static)->newCollection();
}

// Records passed in weren't empty, so process the records
Expand All @@ -159,7 +159,7 @@ public static function createModelsFromRecordSet(BaseCollection $records): Colle
});

// return the filled Eloquent/Collection (or the custom collection specified in the model)
return (new static())->newCollection($mappedRecords->all());
return (new static)->newCollection($mappedRecords->all());
}

/** Fill in data for this existing model with record data from FileMaker
Expand Down
47 changes: 36 additions & 11 deletions src/Services/FileMakerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,24 @@ protected function fetchNewSessionToken()
];

// perform the login
$response = Http::retry($this->attempts, 100)->withBasicAuth($this->config['username'], $this->config['password'])
->post($url, $postBody);
try {
$response = Http::retry($this->attempts, 100)->withBasicAuth($this->config['username'], $this->config['password'])
->post($url, $postBody);
} catch (\Exception $e) {
// log the query even on an error
$this->logFMQuery('post', $url, $postBody, $start);
throw $e;
}

// log the query
$this->logFMQuery('post', $url, $postBody, $start);

// Check for errors
$this->checkResponseForErrors($response);

Arr::set($postBody, 'fmDataSource.0.username', str_repeat('*', strlen(Arr::get($postBody, 'fmDataSource.0.username'))));
Arr::set($postBody, 'fmDataSource.0.password', str_repeat('*', strlen(Arr::get($postBody, 'fmDataSource.0.password'))));

$this->logFMQuery('post', $url, $postBody, $start);

// Get the session token from the response
$token = Arr::get($response, 'response.token');

Expand Down Expand Up @@ -718,7 +725,17 @@ protected function makeRequest($method, $url, $params = [], ?PendingRequest $req
$request = $this->prepareRequestForSending($request);

// make the request
$response = $request->{$method}($url, $params);

try {
$response = $request->{$method}($url, $params);
} catch (\Exception $e) {
// log the query before throwing the exception
$this->logFMQuery($method, $url, $params, $start);
throw $e;
}
// log the query after a successful request
$this->logFMQuery($method, $url, $params, $start);

// Check for errors
try {
$this->checkResponseForErrors($response);
Expand All @@ -730,19 +747,27 @@ protected function makeRequest($method, $url, $params = [], ?PendingRequest $req

// try the request again with refreshed credentials
$request = $this->prepareRequestForSending($request);
$response = $request->{$method}($url, $params);
try {
// execute the request
$response = $request->{$method}($url, $params);
} catch (\Exception $e) {
// log the query before throwing the exception
$this->logFMQuery($method, $url, $params, $start);
throw $e;
}

// log the query after a successful request
$this->logFMQuery($method, $url, $params, $start);

// check for errors a second time, but this time we won't catch the error if there's still an auth
// problem
// check for errors a second time, but this time we won't catch the error if there's
// still an auth problem
$this->checkResponseForErrors($response);

} else {
throw $e;
}
}

$this->logFMQuery($method, $url, $params, $start);

// Return the JSON response
$json = $response->json();

Expand Down Expand Up @@ -809,7 +834,7 @@ public function setRetries($retries)

protected function getDefaultQueryGrammar()
{
return new FMGrammar();
return new FMGrammar;
}

// public function getLayoutMetadata($layout = null)
Expand Down

0 comments on commit c2271d2

Please sign in to comment.