Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug-Fix: Fix ComposeExceptionMessageCodec to include full Response object when decoded from event bus #356

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/iudx/aaa/server/apiserver/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import iudx.aaa.server.apiserver.util.ComposeExceptionMessageCodec;
import iudx.aaa.server.apiserver.util.Urn;

/**
Expand Down Expand Up @@ -108,6 +109,46 @@ public JsonObject toJson() {
return j;
}

/**
* Convert JSON to Response object. Used for {@link ComposeExceptionMessageCodec} to form the
* {@link ComposeException} once it's sent on the wire.
*
* @return a {@link Response} object
*/
public static Response fromJson(JsonObject j) {
ResponseBuilder builder = new ResponseBuilder();

builder.type(j.getString("type"));
builder.title(j.getString("title"));

Object results = j.getValue("results");

if (results != null && results instanceof JsonArray) {
builder.arrayResults((JsonArray) results);
}

if (results != null && results instanceof JsonObject) {
builder.objectResults((JsonObject) results);
}

String detail = j.getString("detail");
if (detail != null) {
builder.detail(detail);
}

int status = j.getInteger("status");
if (status != 0) {
builder.status(status);
}

JsonObject errorContext = j.getJsonObject("context");
if (errorContext != null) {
builder.errorContext(errorContext);
}

return builder.build();
}

public String toJsonString() {
return toJson().encode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.MessageCodec;
import io.vertx.core.json.JsonObject;
import iudx.aaa.server.apiserver.Response;

/**
* Message codec for {@link iudx.aaa.server.apiserver.util.ComposeException}. This is used to decode
Expand Down Expand Up @@ -43,12 +44,7 @@ public ComposeException decodeFromWire(int position, Buffer buffer) {
JsonObject contentJson = new JsonObject(jsonStr);

// We can finally create custom message object
/* TODO: change this so that we use Response.fromJson() or something */
return new ComposeException(
contentJson.getInteger("status"),
contentJson.getString("type"),
contentJson.getString("title"),
contentJson.getString("detail"));
return new ComposeException(Response.fromJson(contentJson));
}

@Override
Expand Down
Loading