Skip to content

Commit

Permalink
Add subcodes to packet status
Browse files Browse the repository at this point in the history
  • Loading branch information
Superioz committed Feb 10, 2021
1 parent 56055a9 commit 5c08551
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ public void onChat(String payload) {

Now if the payload is a number, everything is fine and we would respond with the status **OK**, otherwise we would automatically respond with **INTERNAL** and a message specifying the exception.

Otherwise if we want to specify a status ourselves, we can use the following:

```java
@HagridListens(topic = "chat")
public void onChat(String payload, HagridPacket<?> req, HagridResponse res) {
try {
res.status(StatusCode.OK, "everything is fine.");
} catch (NumberFormatException ex) {
res.status(StatusCode.BAD_REQUEST, "you have not sent a number");
return;
}
System.out.println("We received a number: " + someNumber);
}
```

That way after the listener has been executed, it will automatically construct a needed response with the status given.
To add some kind of more detailed description of what happened, you can use something called `subcodes`.

```java
@HagridListens(topic = "chat")
public void onChat(String payload, HagridPacket<?> req, HagridResponse res) {
try {
res.status(StatusCode.OK, "everything is fine.");
} catch (NumberFormatException ex) {
// adding a subcode as an enum or an int to the status
res.status(StatusCode.BAD_REQUEST, ChatSubCode.NOT_A_NUMBER, "you have not sent a number");
return;
}
System.out.println("We received a number: " + someNumber);
}
```

In this case `ChatSubCode` is an enum created by the application, which then gets transformed into an int by the ordinal - more specifically `ordinal() + 1` as the subcode starts counting at 1.
Now the receiver of the response can also use `ChatSubCode` to get a better idea of what `BAD_REQUEST` means specifically without having to map the message to a specific status.

## Taking advantage of topic patterns

As explained above topics on Hagrid's site can be seen as a pattern. This can become useful if we e.g. have chatrooms and we want to listen to all of them.
Expand Down
2 changes: 1 addition & 1 deletion hagrid-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>hagrid-api</artifactId>
<version>1.4.2</version>
<version>1.5.0</version>

<dependencies>
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,28 @@ public class HagridResponse {
private Status status;
private Object payload;

public HagridResponse status(final StatusCode code, final String message) {
this.status = new Status(code, message);
public HagridResponse status(final StatusCode code, final int subcode, final String message) {
this.status = new Status(code, subcode, message);
return this;
}

public HagridResponse status(final StatusCode code, final Enum<?> subcode, final String message) {
this.status = new Status(code, subcode.ordinal() + 1, message);
return this;
}

public HagridResponse status(final StatusCode code, final int subcode) {
return this.status(code, subcode, "");
}

public HagridResponse status(final StatusCode code, final Enum<?> subcode) {
return this.status(code, subcode.ordinal() + 1, "");
}

public HagridResponse status(final StatusCode code, final String message) {
return this.status(code, 0, message);
}

public HagridResponse status(final StatusCode code) {
return this.status(code, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ public class Status {
*/
private final StatusCode code;

/**
* An application specific subcode.
*/
private final int subcode;

/**
* Optional message of the status. To not send a message
* just leave it empty.
*/
private final String message;

public Status(final StatusCode code, final String message) {
public Status(final StatusCode code, final int subcode, final String message) {
this.code = code;
this.subcode = subcode;
this.message = message;
}

public Status(final StatusCode code, final String message) {
this(code, 0, message);
}

public Status(final StatusCode code) {
this(code, "");
}
Expand All @@ -42,16 +52,39 @@ public boolean isTimeout() {
return this.is(StatusCode.TIMEOUT);
}

public boolean is(final int subcode) {
if (this.subcode == 0) return false;
return this.subcode == subcode;
}

public boolean is(final Enum<?> en) {
return this.is(en.ordinal() + 1);
}

public boolean is(final StatusCode code, final int subcode) {
return this.is(code) && this.is(subcode);
}

public boolean is(final StatusCode code, final Enum<?> en) {
return this.is(code) && this.is(en);
}

public StatusCode getCode() {
return this.code;
}

public int getSubcode() {
return this.subcode;
}

public String getMessage() {
return this.message;
}

@Override
public String toString() {
return this.code.name() + (this.message != null && !this.message.isEmpty() ? ": " + this.message : "");
return this.code.name()
+ "(" + this.subcode + ")"
+ (this.message != null && !this.message.isEmpty() ? ": " + this.message : "");
}
}
4 changes: 2 additions & 2 deletions hagrid-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>hagrid-core</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>

<dependencies>
<dependency>
Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>dev.volix.rewinside.odyssey.hagrid</groupId>
<artifactId>hagrid-api</artifactId>
<version>1.4.2</version>
<version>1.5.0</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion hagrid-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>dev.volix.rewinside.odyssey.hagrid</groupId>
<artifactId>hagrid-core</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 5c08551

Please sign in to comment.