diff --git a/templates/node/src/client.ts.twig b/templates/node/src/client.ts.twig index effca20f08..05140f4a02 100644 --- a/templates/node/src/client.ts.twig +++ b/templates/node/src/client.ts.twig @@ -646,6 +646,28 @@ class Client { redirect: 'manual', }); + if (400 <= response.status) { + const responseText = await response.text(); + let data: any = { message: responseText }; + if ( + response.headers + .get('content-type') + ?.includes('application/json') + ) { + try { + data = JSONbig.parse(responseText); + } catch { + // Keep the raw body as the message when the JSON is malformed + } + } + throw new {{spec.info.title | caseUcfirst}}Exception( + data?.message, + response.status, + data?.type, + responseText, + ); + } + if (response.status !== 301 && response.status !== 302) { throw new {{spec.info.title | caseUcfirst}}Exception('Invalid redirect', response.status); } diff --git a/tests/e2e/Base.php b/tests/e2e/Base.php index 58f350c52d..68a668c42e 100644 --- a/tests/e2e/Base.php +++ b/tests/e2e/Base.php @@ -72,6 +72,11 @@ abstract class Base extends TestCase 'https://localhost?code=abcdef&state=123456', ]; + protected const OAUTH_EXCEPTION_RESPONSES = [ + 'Invalid `clientId` param: Value must be a valid string and at least 1 chars and no longer than 100 chars', + '{"message":"Invalid `clientId` param: Value must be a valid string and at least 1 chars and no longer than 100 chars","code":400}', + ]; + protected const DOWNLOAD_RESPONSES = [ 'GET:/v1/mock/tests/general/download:passed', ]; diff --git a/tests/e2e/Node18Test.php b/tests/e2e/Node18Test.php index 324c88c8cb..8292931a57 100644 --- a/tests/e2e/Node18Test.php +++ b/tests/e2e/Node18Test.php @@ -59,6 +59,7 @@ final class Node18Test extends Base ...Base::OPTIONAL_PARAM_RESPONSES, ...Base::EXCEPTION_RESPONSES, ...Base::OAUTH_RESPONSES, + ...Base::OAUTH_EXCEPTION_RESPONSES, ...Base::QUERY_HELPER_RESPONSES, ...Base::PERMISSION_HELPER_RESPONSES, ...Base::ID_HELPER_RESPONSES, diff --git a/tests/e2e/Node20Test.php b/tests/e2e/Node20Test.php index d921039a30..63ee0af679 100644 --- a/tests/e2e/Node20Test.php +++ b/tests/e2e/Node20Test.php @@ -59,6 +59,7 @@ final class Node20Test extends Base ...Base::OPTIONAL_PARAM_RESPONSES, ...Base::EXCEPTION_RESPONSES, ...Base::OAUTH_RESPONSES, + ...Base::OAUTH_EXCEPTION_RESPONSES, ...Base::QUERY_HELPER_RESPONSES, ...Base::PERMISSION_HELPER_RESPONSES, ...Base::ID_HELPER_RESPONSES, diff --git a/tests/e2e/Node26Test.php b/tests/e2e/Node26Test.php index 207440dd5d..0c67c8ca75 100644 --- a/tests/e2e/Node26Test.php +++ b/tests/e2e/Node26Test.php @@ -59,6 +59,7 @@ final class Node26Test extends Base ...Base::OPTIONAL_PARAM_RESPONSES, ...Base::EXCEPTION_RESPONSES, ...Base::OAUTH_RESPONSES, + ...Base::OAUTH_EXCEPTION_RESPONSES, ...Base::QUERY_HELPER_RESPONSES, ...Base::PERMISSION_HELPER_RESPONSES, ...Base::ID_HELPER_RESPONSES, diff --git a/tests/e2e/languages/node/test.js b/tests/e2e/languages/node/test.js index e3a03aad7b..79b7f7dd42 100644 --- a/tests/e2e/languages/node/test.js +++ b/tests/e2e/languages/node/test.js @@ -316,6 +316,19 @@ async function start() { ) console.log(url) + try { + await general.oauth2( + 'x'.repeat(101), + ['test'], + '123456', + 'https://localhost', + 'https://localhost' + ); + } catch(error) { + console.log(error.message); + console.log(error.response); + } + // Query helper tests console.log(Query.equal("released", [true])); console.log(Query.equal("title", ["Spiderman", "Dr. Strange"]));