Description
Hello,
I found out a way to translate this package's response strings in laravel/passport
package, but there's something I can't figure out and I need your thoughts about it.
The exception response body that oauth2-server provides contains:
[
"error" => $key
"message" => $message,
"hint" => $hint
]
which the error
parameter I can use as key for translating the string into another languages. But, this package has dynamic messages using same keys and I want to distinguish between them.
For example;
OAuthServerException.php line 131-158:
/**
* Server error.
*
* @param $hint
*
* @return static
*
* @codeCoverageIgnore
*/
public static function serverError($hint)
{
return new static(
'The authorization server encountered an unexpected condition which prevented it from fulfilling'
. ' the request: ' . $hint,
7,
'server_error',
500
);
}
The server_error
key might be a lot of things, because the hint
parameter is concatenated to the error message.
Also,
OauthServerException.php line 151-161:
/**
* Invalid refresh token.
*
* @param null|string $hint
*
* @return static
*/
public static function invalidRefreshToken($hint = null)
{
return new static('The refresh token is invalid.', 8, 'invalid_request', 401, $hint);
}
and
OauthServerException.php line 68-83
/**
* Invalid request error.
*
* @param string $parameter The invalid parameter
* @param null|string $hint
*
* @return static
*/
public static function invalidRequest($parameter, $hint = null)
{
$errorMessage = 'The request is missing a required parameter, includes an invalid parameter value, ' .
'includes a parameter more than once, or is otherwise malformed.';
$hint = ($hint === null) ? sprintf('Check the `%s` parameter', $parameter) : $hint;
return new static($errorMessage, 3, 'invalid_request', 400, $hint);
}
have the same key as invalid_request
.
May I change that or add more keys to the Response object which differentiates the error messages?
For example, I'm thinking of doing something like this:
A response object example:
[
"error" => "invalid_request",
"message" => "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.",
"hint" => "Check the `code` parameter",
"translatable" => [
"error_key" => "invalid_request.code",
"hint_key" => "invalid_request.code.hint",
"error_parameters" => [],
"hint_parameters" => [ "code" ]
]
]
Which then I can get a translation string (for [response]->translatable->hintkey
) like "Check the %s
parameter" and fill it with hint_parameters
array. Then the receiving package (like laravel\passport
) can dismiss the "translatable" key from the response.
What do you think?