-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix Error Handling in Functions with API Requests #11
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eeebce2
create TokenRefreshApiController
yutomoriyasu e386444
use setcookie
yutomoriyasu 0d926eb
set refresh token in CallbackController
yutomoriyasu 576cd62
change cookie path
yutomoriyasu d8b4953
revert changes in CallbackController
yutomoriyasu 87dbc03
imple error handling
yutomoriyasu 7d0f2f7
use type and message for error response
yutomoriyasu 6d9173e
remove use statements of unused packages
yutomoriyasu 3577e8d
add uncaught error log
yutomoriyasu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace AntiPatternInc\Saasus\Laravel\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
use Illuminate\Foundation\Validation\ValidatesRequests; | ||
use Illuminate\Routing\Controller as BaseController; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
use AntiPatternInc\Saasus\Api\Client as ApiClient; | ||
use Error; | ||
use Http\Client\Exception\HttpException; | ||
|
||
class TokenRefreshApiController extends BaseController | ||
{ | ||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | ||
|
||
public function index(Request $request) | ||
{ | ||
$refreshToken = $request->cookie('saasus_refresh_token'); | ||
if (empty($refreshToken)) { | ||
return response()->json('saasus_refresh_token cookie is required', Response::HTTP_BAD_REQUEST); | ||
} | ||
$client = new ApiClient(); | ||
$authApi = $client->getAuthClient(); | ||
try { | ||
$res = $authApi->getAuthCredentials([ | ||
'refresh-token' => $refreshToken, 'auth-flow' => 'refreshTokenAuth', | ||
], $authApi::FETCH_RESPONSE); | ||
$body = json_decode($res->getBody(), true); | ||
if (empty($body['id_token']) || empty($body['access_token'])) { | ||
throw new Error('failed to get new credentials'); | ||
} | ||
return response()->json($body, Response::HTTP_OK); | ||
} catch (\Exception $e) { | ||
if ($e instanceof HttpException) { | ||
$statusCode = $e->getResponse()->getStatusCode(); | ||
$type = json_decode($e->getResponse()->getBody(), true)["type"]; | ||
$message = json_decode($e->getResponse()->getBody(), true)["message"]; | ||
if ($statusCode == Response::HTTP_NOT_FOUND) { | ||
Log::info('Type: ' . $type . ', Message: ' . $message); | ||
return response()->json(['type' => $type, 'message' => $message], Response::HTTP_NOT_FOUND); | ||
} | ||
Log::info('Type: ' . $type . ', Message: ' . $message); | ||
return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); | ||
} | ||
Log::info('Uncaught error: ' . $e); | ||
return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HttpException以外のケースがわかってないですが
ログ出力なしで良さそうかだけ確認させてください〜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ログ出すようにしました!3577e8d