Skip to content

Commit

Permalink
fix: update logs sent on groovy script error
Browse files Browse the repository at this point in the history
  • Loading branch information
Okhelifi committed Mar 27, 2024
1 parent 390234c commit 400762c
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/gravitee/policy/groovy/GroovyPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public ReadWriteStream onResponseContent(
}
} catch (Throwable t) {
logger.error("Unable to run Groovy script", t);
throw new TransformationException("Unable to run Groovy script: " + t.getMessage(), t);
throw new TransformationException("Internal Server Error");
}
return null;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public ReadWriteStream onRequestContent(
}
} catch (Throwable t) {
logger.error("Unable to run Groovy script", t);
throw new TransformationException("Unable to run Groovy script: " + t.getMessage(), t);
throw new TransformationException("Internal Server Error");
}
return null;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private String executeScript(
}
} catch (Throwable t) {
logger.error("Unable to run Groovy script", t);
policyChain.failWith(io.gravitee.policy.api.PolicyResult.failure(t.getMessage()));
policyChain.failWith(io.gravitee.policy.api.PolicyResult.failure("Internal Server Error"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,88 @@ void should_use_response_template_when_key_provided(WebClient client) {

wiremock.verify(0, postRequestedFor(urlPathEqualTo("/team")));
}

@Test
@DeployApi("/apis/api-fail-parse-response-content.json")
void should_throw_exception_on_response_content(WebClient client) {
wiremock.stubFor(post("/team").willReturn(ok("[{\"foo\", \"bar\"},\"bar\": \"baz\"]")));

client
.post("/test")
.rxSend()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertValue(
response -> {
assertThat(response.statusCode()).isEqualTo(500);
assertThat(response.bodyAsString()).contains("Internal Server Error");
return true;
}
)
.assertComplete()
.assertNoErrors();
}

@Test
@DeployApi("/apis/api-fail-parse-request-content.json")
void should_throw_exception_on_request_content(WebClient client) {
wiremock.stubFor(post("/team").willReturn(ok("")));

client
.post("/test")
.rxSendJson("[{\"foo\": \"bar\"}, {\"bar\": \"baz\"}]")
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertValue(
response -> {
assertThat(response.statusCode()).isEqualTo(500);
assertThat(response.bodyAsString()).contains("Internal Server Error");
return true;
}
)
.assertComplete()
.assertNoErrors();
}

@Test
@DeployApi("/apis/api-fail-execute-response-script.json")
void should_throw_exception_on_response(WebClient client) {
wiremock.stubFor(post("/team").willReturn(ok("")));

client
.post("/test")
.rxSend()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertValue(
response -> {
assertThat(response.statusCode()).isEqualTo(500);
assertThat(response.bodyAsString()).contains("Internal Server Error");
return true;
}
)
.assertComplete()
.assertNoErrors();
}

@Test
@DeployApi("/apis/api-fail-execute-request-script.json")
void should_throw_exception_on_request(WebClient client) {
wiremock.stubFor(post("/team").willReturn(ok("")));

client
.post("/test")
.rxSend()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertValue(
response -> {
assertThat(response.statusCode()).isEqualTo(500);
assertThat(response.bodyAsString()).contains("Internal Server Error");
return true;
}
)
.assertComplete()
.assertNoErrors();
}
}
44 changes: 44 additions & 0 deletions src/test/resources/apis/api-fail-execute-request-script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"id": "api-fail-response-template",
"name": "my-api",
"gravitee": "2.0.0",
"proxy": {
"context_path": "/test",
"endpoints": [
{
"name": "default",
"target": "http://localhost:8080/team",
"http": {
"connectTimeout": 3000,
"readTimeout": 60000
}
}
]
},
"flows": [
{
"name": "flow-1",
"methods": [
],
"enabled": true,
"path-operator": {
"path": "/",
"operator": "STARTS_WITH"
},
"pre": [
{
"name": "Groovy",
"description": "",
"enabled": true,
"policy": "groovy",
"configuration": {
"scope": "REQUEST",
"onRequestScript": "def foo= nes azdazdaa();"
}
}
],
"post": []
}
],
"resources": []
}
44 changes: 44 additions & 0 deletions src/test/resources/apis/api-fail-execute-response-script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"id": "api-fail-response-template",
"name": "my-api",
"gravitee": "2.0.0",
"proxy": {
"context_path": "/test",
"endpoints": [
{
"name": "default",
"target": "http://localhost:8080/team",
"http": {
"connectTimeout": 3000,
"readTimeout": 60000
}
}
]
},
"flows": [
{
"name": "flow-1",
"methods": [
],
"enabled": true,
"path-operator": {
"path": "/",
"operator": "STARTS_WITH"
},
"pre": [],
"post": [
{
"name": "Groovy",
"description": "",
"enabled": true,
"policy": "groovy",
"configuration": {
"scope": "RESPONSE",
"onResponseScript": "def foo= azkcaznc();"
}
}
]
}
],
"resources": []
}
44 changes: 44 additions & 0 deletions src/test/resources/apis/api-fail-parse-request-content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"id": "api-fail-response-template",
"name": "my-api",
"gravitee": "2.0.0",
"proxy": {
"context_path": "/test",
"endpoints": [
{
"name": "default",
"target": "http://localhost:8080/team",
"http": {
"connectTimeout": 3000,
"readTimeout": 60000
}
}
]
},
"flows": [
{
"name": "flow-1",
"methods": [
],
"enabled": true,
"path-operator": {
"path": "/",
"operator": "STARTS_WITH"
},
"pre": [
{
"name": "Groovy",
"description": "",
"enabled": true,
"policy": "groovy",
"configuration": {
"scope": "REQUEST",
"onRequestContentScript": "import groovy.json.JsonSlurper\nimport groovy.json.JsonOutput\n\ndef jsonSlurper = new JsonSlurper()\ndef content = jsonSlurper.parseText(response.content)\nreturn JsonOutput.toJson(content)"
}
}
],
"post": []
}
],
"resources": []
}
44 changes: 44 additions & 0 deletions src/test/resources/apis/api-fail-parse-response-content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"id": "api-fail-response-template",
"name": "my-api",
"gravitee": "2.0.0",
"proxy": {
"context_path": "/test",
"endpoints": [
{
"name": "default",
"target": "http://localhost:8080/team",
"http": {
"connectTimeout": 3000,
"readTimeout": 60000
}
}
]
},
"flows": [
{
"name": "flow-1",
"methods": [
],
"enabled": true,
"path-operator": {
"path": "/",
"operator": "STARTS_WITH"
},
"pre": [],
"post": [
{
"name": "Groovy",
"description": "",
"enabled": true,
"policy": "groovy",
"configuration": {
"scope": "RESPONSE",
"onResponseContentScript": "import groovy.json.JsonSlurper\nimport groovy.json.JsonOutput\n\ndef jsonSlurper = new JsonSlurper()\ndef content = jsonSlurper.parseText(response.content)\nreturn JsonOutput.toJson(content)"
}
}
]
}
],
"resources": []
}

0 comments on commit 400762c

Please sign in to comment.