Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log API requests, even on errors #77

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading