Skip to content

Commit acbe089

Browse files
Standardize error message handling (#289)
* Put error responses in a JSON formatted response
1 parent e632d92 commit acbe089

21 files changed

+187
-130
lines changed

src/functionaltests/java/com/ericsson/ei/subscriptions/authentication/AuthenticationSteps.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ public void subscription_with_name_created(String check) throws Throwable {
112112
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint("/subscriptions/" + SUBSCRIPTION_NAME);
113113

114114
response = httpRequest.performRequest();
115-
GetSubscriptionResponse subscription = new ObjectMapper().readValue(response.getBody().toString(),
116-
GetSubscriptionResponse.class);
117115
if (!check.isEmpty()) {
116+
JSONObject jsonResponse = new JSONObject(response.getBody().toString());
117+
String errorMessage = jsonResponse.getString("message");
118118
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
119-
assertEquals(true, subscription.getFoundSubscriptions().isEmpty());
120-
assertEquals(SUBSCRIPTION_NAME, subscription.getNotFoundSubscriptions().get(0));
119+
assertEquals(true, errorMessage.contains(SUBSCRIPTION_NAME));
121120
} else {
121+
GetSubscriptionResponse subscription = new ObjectMapper().readValue(response.getBody().toString(),
122+
GetSubscriptionResponse.class);
122123
assertEquals(HttpStatus.OK, response.getStatusCode());
123124
assertEquals(true, subscription.getNotFoundSubscriptions().isEmpty());
124125
assertEquals(SUBSCRIPTION_NAME, subscription.getFoundSubscriptions().get(0).getSubscriptionName());

src/functionaltests/java/com/ericsson/ei/subscriptions/bulk/SubscriptionBulkSteps.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ public void get_in_response_content_subscription_and_reason(String subscriptionN
116116
assertEquals(subscriptionName, jsonResponse.getString("subscription"));
117117
}
118118

119+
@Then("^get in response content subscription \"([^\"]*)\" expecting an error$")
120+
public void get_in_response_content_subscription_and_reason_with_error(String subscriptionName) throws Throwable {
121+
JSONObject jsonResponse = new JSONObject(response.getBody().toString());
122+
String errorMessage = jsonResponse.getString("message");
123+
assertEquals(true, errorMessage.contains(subscriptionName));
124+
}
125+
119126
@Then("^number of retrieved subscriptions using REST API \"([^\"]*)\" is (\\d+)$")
120127
public void number_of_retrieved_subscriptions_using_REST_API_is(
121128
String endpoint, int subscriptionsNumber) throws Throwable {

src/functionaltests/java/com/ericsson/ei/subscriptions/crud/SubscriptionCRUDSteps.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,17 @@ public void i_make_a_DELETE_request_with_subscription_name_to_the_subscription_R
120120
response = httpRequest.performRequest();
121121
}
122122

123-
@Then("^My GET request with subscription name \"([^\"]*)\" at REST API \"([^\"]*)\" returns an empty String$")
123+
@Then("^My GET request with subscription name \"([^\"]*)\" at REST API \"([^\"]*)\" returns an error message$")
124124
public void my_GET_request_with_subscription_name_at_REST_API_returns_empty_String(String name, String endPoint)
125125
throws Throwable {
126126
httpRequest = new HttpRequest(HttpMethod.GET);
127127
httpRequest.setHost(hostName).setPort(applicationPort).setEndpoint(endPoint + name)
128128
.addHeader("Accept", "application/json");
129129
response = httpRequest.performRequest();
130-
JsonNode node = eventManager.getJSONFromString(response.getBody());
131-
int size = node.get("foundSubscriptions").size();
132-
String notFound = node.get("notFoundSubscriptions").get(0).asText();
133-
assertEquals("Subscription was found, should be empty",0, size);
134-
assertEquals(name, notFound);
130+
JSONObject jsonResponse = new JSONObject(response.getBody().toString());
131+
String errorMessage = jsonResponse.getString("message");
132+
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
133+
assertEquals(true, errorMessage.contains(name));
135134
}
136135
// Scenario:4 ends ==================================================================
137136
}

src/functionaltests/resources/features/subscriptionBulk.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Test Subscription Bulk Operations
2222
Given file with subscriptions "/subscriptions_multiple_wrong.json"
2323
When make a POST request with list of subscriptions to the subscription REST API "/subscriptions"
2424
Then get response code of 400
25-
And get in response content subscription "Subscription_Test_2"
25+
And get in response content subscription "Subscription_Test_2" expecting an error
2626
And number of retrieved subscriptions using REST API "/subscriptions" is 5
2727

2828
@DeleteSubscriptions
@@ -44,12 +44,12 @@ Feature: Test Subscription Bulk Operations
4444
Given file with subscriptions "/subscriptions_multiple_wrong_updated.json"
4545
When make a PUT request with list of subscriptions to the subscription REST API "/subscriptions"
4646
Then get response code of 400
47-
And get in response content subscription "Subscription_Test_Not_Found"
47+
And get in response content subscription "Subscription_Test_Not_Found" expecting an error
4848
And number of retrieved subscriptions using REST API "/subscriptions" is 3
4949

5050
@DeleteSubscriptionsNonExisting
5151
Scenario: Delete multiple subscriptions using REST API, one subscription does not exist
5252
When make a DELETE request with list of subscriptions names "Subscription_Test_1,Subscription_Test_2,Subscription_Test_Not_Found,Subscription_Test_3" to the subscription REST API "/subscriptions"
5353
Then get response code of 400
54-
And get in response content subscription "Subscription_Test_Not_Found"
54+
And get in response content subscription "Subscription_Test_Not_Found" expecting an error
5555
And number of retrieved subscriptions using REST API "/subscriptions" is 0

src/functionaltests/resources/features/subscriptionCRUD.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ Feature: Test Subscription CRUD
3333
Given The REST API "/subscriptions" is up and running
3434
When I make a DELETE request with subscription name "Subscription_Test" to the subscription REST API "/subscriptions/"
3535
Then I get response code of 200
36-
And My GET request with subscription name "Subscription_Test" at REST API "/subscriptions/" returns an empty String
36+
And My GET request with subscription name "Subscription_Test" at REST API "/subscriptions/" returns an error message

src/main/java/com/ericsson/ei/controller/AuthControllerImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.stereotype.Component;
2727
import org.springframework.web.bind.annotation.CrossOrigin;
2828

29+
import com.ericsson.ei.utils.ResponseMessage;
30+
2931
import io.swagger.annotations.Api;
3032
import io.swagger.annotations.ApiOperation;
3133

@@ -50,9 +52,10 @@ public ResponseEntity<?> getAuth() {
5052
try {
5153
return new ResponseEntity<>(new JSONObject().put("security", ldapEnabled).toString(), HttpStatus.OK);
5254
} catch (Exception e) {
53-
String errorMessage = "Failed to check if security is enabled. Error message:\n" + e.getMessage();
55+
String errorMessage = "Internal Server Error: Failed to check if security is enabled.";
5456
LOGGER.error(errorMessage, e);
55-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
57+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
58+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
5659
}
5760
}
5861

@@ -64,9 +67,10 @@ public ResponseEntity<?> getAuthLogin() {
6467
String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
6568
return new ResponseEntity<>(new JSONObject().put("user", currentUser).toString(), HttpStatus.OK);
6669
} catch (Exception e) {
67-
String errorMessage = "Failed to log in user. Error message:\n" + e.getMessage();
70+
String errorMessage = "Internal Server Error: Failed to log in user.";
6871
LOGGER.error(errorMessage, e);
69-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
72+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
73+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
7074
}
7175
}
7276

@@ -77,9 +81,10 @@ public ResponseEntity<?> getAuthCheckStatus() {
7781
try {
7882
return new ResponseEntity<>("Backend server is up and running", HttpStatus.OK);
7983
} catch (Exception e) {
80-
String errorMessage = "Failed to check backend status. Error message:\n" + e.getMessage();
84+
String errorMessage = "Internal Server Error: Failed to check backend status.";
8185
LOGGER.error(errorMessage, e);
82-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
86+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
87+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
8388
}
8489
}
8590
}

src/main/java/com/ericsson/ei/controller/DownloadControllerImpl.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package com.ericsson.ei.controller;
1818

19-
import java.io.IOException;
2019
import java.io.InputStream;
2120

2221
import org.apache.commons.io.IOUtils;
@@ -28,6 +27,8 @@
2827
import org.springframework.stereotype.Component;
2928
import org.springframework.web.bind.annotation.CrossOrigin;
3029

30+
import com.ericsson.ei.utils.ResponseMessage;
31+
3132
import io.swagger.annotations.Api;
3233

3334
@Component
@@ -46,54 +47,64 @@ public ResponseEntity<?> getDownload() {
4647
response.put("events", "/download/eventsTemplate");
4748
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
4849
} catch (Exception e) {
49-
String errorMessage = "Failed to get information about download endpoints. Error message:\n" + e.getMessage();
50+
String errorMessage = "Internal Server Error: Failed to get information about download endpoints.";
5051
LOGGER.error(errorMessage, e);
51-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
52+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
53+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
5254
}
5355
}
5456

5557
@Override
5658
public ResponseEntity<?> getDownloadSubscriptionsTemplate() {
5759
try {
5860
InputStream is = getClass().getResourceAsStream("/templates/subscriptionsTemplate.json");
61+
if (is == null) {
62+
String errorMessage = "Subscriptions template file is not found.";
63+
LOGGER.error(errorMessage);
64+
return new ResponseEntity<>(ResponseMessage.createJsonMessage(errorMessage), HttpStatus.NOT_FOUND);
65+
}
5966
return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK);
60-
} catch (IOException e) {
61-
LOGGER.error(e.getMessage(), e);
62-
return new ResponseEntity<>("Subscriptions template file is not found", HttpStatus.NOT_FOUND);
6367
} catch (Exception e) {
64-
String errorMessage = "Failed to download subscriptions template file. Error message:\n" + e.getMessage();
68+
String errorMessage = "Internal Server Error: Failed to download subscriptions template file.";
6569
LOGGER.error(e.getMessage(), e);
66-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
70+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
71+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
6772
}
6873
}
6974

7075
@Override
7176
public ResponseEntity<?> getDownloadRulesTemplate() {
7277
try {
7378
InputStream is = getClass().getResourceAsStream("/templates/rulesTemplate.json");
79+
if (is == null) {
80+
String errorMessage = "Rules template file is not found.";
81+
LOGGER.error(errorMessage);
82+
return new ResponseEntity<>(ResponseMessage.createJsonMessage(errorMessage), HttpStatus.NOT_FOUND);
83+
}
7484
return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK);
75-
} catch (IOException e) {
76-
LOGGER.error(e.getMessage(), e);
77-
return new ResponseEntity<>("Rules template file is not found", HttpStatus.NOT_FOUND);
7885
} catch (Exception e) {
79-
String errorMessage = "Failed to download rules template file. Error message:\n" + e.getMessage();
86+
String errorMessage = "Internal Server Error: Failed to download rules template file.";
8087
LOGGER.error(errorMessage, e);
81-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
88+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
89+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
8290
}
8391
}
8492

8593
@Override
8694
public ResponseEntity<?> getDownloadEventsTemplate() {
8795
try {
8896
InputStream is = getClass().getResourceAsStream("/templates/eventsTemplate.json");
97+
if (is == null) {
98+
String errorMessage = "Events template file is not found.";
99+
LOGGER.error(errorMessage);
100+
return new ResponseEntity<>(ResponseMessage.createJsonMessage(errorMessage), HttpStatus.NOT_FOUND);
101+
}
89102
return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK);
90-
} catch (IOException e) {
91-
LOGGER.error(e.getMessage(), e);
92-
return new ResponseEntity<>("Events template file is not found", HttpStatus.NOT_FOUND);
93103
} catch (Exception e) {
94-
String errorMessage = "Failed to download events template file. Error message:\n" + e.getMessage();
104+
String errorMessage = "Internal Server Error: Failed to download events template file.";
95105
LOGGER.error(errorMessage, e);
96-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
106+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
107+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
97108
}
98109
}
99110
}

src/main/java/com/ericsson/ei/controller/InformationControllerImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
package com.ericsson.ei.controller;
1515

1616
import com.ericsson.ei.controller.model.ParseInstanceInfoEI;
17+
import com.ericsson.ei.utils.ResponseMessage;
1718
import com.fasterxml.jackson.databind.ObjectMapper;
1819
import io.swagger.annotations.Api;
1920
import io.swagger.annotations.ApiOperation;
21+
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,9 +45,10 @@ public ResponseEntity<?> getInformation() {
4345
LOGGER.debug("EI backend information is parsed successfully");
4446
return new ResponseEntity<>(info, HttpStatus.OK);
4547
} catch (Exception e) {
46-
String errorMessage = "Failed to parse EI backend information. Error message:\n" + e.getMessage();
47-
LOGGER.error(errorMessage);
48-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
48+
String errorMessage = "Internal Server Error: Failed to parse EI backend information.";
49+
LOGGER.error(errorMessage, e);
50+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
51+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
4952
}
5053
}
5154
}

src/main/java/com/ericsson/ei/controller/QueryAggregatedObjectController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.ericsson.ei.controller;
33

4-
import com.ericsson.ei.controller.model.QueryResponse;
54
import org.springframework.http.ResponseEntity;
65
import org.springframework.validation.annotation.Validated;
76
import org.springframework.web.bind.annotation.RequestMapping;
@@ -26,7 +25,7 @@ public interface QueryAggregatedObjectController {
2625
*
2726
*/
2827
@RequestMapping(value = "", method = RequestMethod.GET)
29-
public ResponseEntity<QueryResponse> getQueryAggregatedObject(
28+
public ResponseEntity<?> getQueryAggregatedObject(
3029
@RequestParam
3130
String id);
3231

src/main/java/com/ericsson/ei/controller/QueryAggregatedObjectControllerImpl.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
import com.ericsson.ei.controller.model.QueryResponse;
2828
import com.ericsson.ei.controller.model.QueryResponseEntity;
2929
import com.ericsson.ei.queryservice.ProcessAggregatedObject;
30+
import com.ericsson.ei.utils.ResponseMessage;
3031
import com.fasterxml.jackson.databind.ObjectMapper;
3132

32-
3333
/**
34-
* This class represents the REST GET mechanism to extract the aggregated data
35-
* on the basis of the ID from the aggregatedObject.
34+
* This class represents the REST GET mechanism to extract the aggregated data on the basis of the ID from the aggregatedObject.
3635
*/
3736
@Component
3837
@CrossOrigin
@@ -44,14 +43,13 @@ public class QueryAggregatedObjectControllerImpl implements QueryAggregatedObjec
4443
private ProcessAggregatedObject processAggregatedObject;
4544

4645
/**
47-
* This method is responsible for the REST Get mechanism to extract the
48-
* aggregated data on the basis of the ID from the aggregatedObject.
46+
* This method is responsible for the REST Get mechanism to extract the aggregated data on the basis of the ID from the aggregatedObject.
4947
*
5048
* @param id
5149
* @return ResponseEntity
5250
*/
5351
@Override
54-
public ResponseEntity<QueryResponse> getQueryAggregatedObject(@RequestParam("ID") final String id) {
52+
public ResponseEntity<?> getQueryAggregatedObject(@RequestParam("ID") final String id) {
5553
ObjectMapper mapper = new ObjectMapper();
5654
QueryResponseEntity queryResponseEntity = new QueryResponseEntity();
5755
QueryResponse queryResponse = new QueryResponse();
@@ -65,13 +63,12 @@ public ResponseEntity<QueryResponse> getQueryAggregatedObject(@RequestParam("ID"
6563

6664
queryResponse.setQueryResponseEntity(queryResponseEntity);
6765
LOGGER.debug("The response is: " + response.toString());
68-
return new ResponseEntity<>(queryResponse, httpStatus);
66+
return new ResponseEntity<QueryResponse>(queryResponse, httpStatus);
6967
} catch (Exception e) {
70-
String errorMessage = "Failed to extract the aggregated data from the Aggregated Object based on ID " + id
71-
+ ". Error message:\n" + e.getMessage();
68+
String errorMessage = "Internal Server Error: Failed to extract the aggregated data from the Aggregated Object based on ID " + id + ".";
7269
LOGGER.error(errorMessage, e);
73-
queryResponse.setAdditionalProperty("errorMessage", errorMessage);
74-
return new ResponseEntity<>(queryResponse, HttpStatus.INTERNAL_SERVER_ERROR);
70+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
71+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
7572
}
7673
}
7774
}

0 commit comments

Comments
 (0)