Skip to content

Commit

Permalink
Monitoring API now correctly returns a 404 when the session is not found
Browse files Browse the repository at this point in the history
Fix #279
  • Loading branch information
gdaniel committed Feb 13, 2020
1 parent a489399 commit 14fd9b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e
- `RestHandler` instances can now throw a `RestHandlerException` to notify the server that an error occurred when handling the request. For now this exception is used to return a *404* status code instead of *200*.
- Change log level of non-critical messages in `XatkitServer`, `DialogFlow` and `RegEx` intent recognition providers. This reduces the amount of noise in Xatkit logs.
- `RestHandlerException` now supports HTTP error codes, and the `HttpHandler` uses this value to return the corresponding HTTP status to the client.
- Monitoring endpoint `GET: /analytics/monitoring/session` now returns a `404` with an error message if the `sessionId` parameter is missing or if the corresponding session cannot be found. **This change breaks the public API**: client application expecting a status code `200` should be updated.

## Removed

Expand All @@ -38,6 +39,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e
- [#252](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/252): *Pre/Post processing of user inputs*
- [#265](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/265): *An option to remove stop words from parameters extracted with any*
- [#271](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/271): *Add support for composite entity*
- [#279](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/279): *Monitoring API: error responses should use proper HTTP codes*

## [4.0.0] - 2019-12-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonPrimitive;
import com.xatkit.core.server.HttpMethod;
import com.xatkit.core.server.HttpUtils;
import com.xatkit.core.server.RestHandlerException;
import com.xatkit.core.server.RestHandlerFactory;
import com.xatkit.core.server.XatkitServer;
import com.xatkit.core.session.XatkitSession;
Expand All @@ -21,7 +22,6 @@
import java.util.TreeMap;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
* Provides monitoring capabilities for {@link IntentRecognitionProvider}s.
Expand Down Expand Up @@ -231,22 +231,32 @@ private void registerGetMonitoringData(XatkitServer xatkitServer) {
* }
* }
* </pre>
* <p>
* <b>Note</b>: this endpoint returns a {@code 404} status and the following content if the provided session does
* not exist:
* <pre>
* {@code
* {
* "error" : "Session <sessionId> not found"
* }
* }
* </pre>
*
* @param xatkitServer the {@link XatkitServer} instance used to register the REST endpoint
*/
private void registerGetMonitoringDataForSession(XatkitServer xatkitServer) {
xatkitServer.registerRestEndpoint(HttpMethod.GET, "/analytics/monitoring/session",
RestHandlerFactory.createJsonRestHandler(((headers, params, content) -> {
String sessionId = HttpUtils.getParameterValue("sessionId", params);
if (nonNull(sessionId)) {
Map<Long, IntentRecord> sessionRecords = records.get(sessionId);
if (nonNull(sessionRecords)) {
return buildSessionObject(sessionId, sessionRecords);
}
if(isNull(sessionId)) {
throw new RestHandlerException(404, "Missing parameter sessionId");
}
Map<Long, IntentRecord> sessionRecords = records.get(sessionId);
if(isNull(sessionRecords)) {
throw new RestHandlerException(404, "Session " + sessionId + " not found");
} else {
return buildSessionObject(sessionId, sessionRecords);
}
JsonObject result = new JsonObject();
result.addProperty("error", "Session " + sessionId + " not found");
return result;
})));
}

Expand Down

0 comments on commit 14fd9b1

Please sign in to comment.