aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/ymc/acron/net/WSFrameHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/moe/ymc/acron/net/WSFrameHandler.java')
-rw-r--r--src/main/java/moe/ymc/acron/net/WSFrameHandler.java17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/main/java/moe/ymc/acron/net/WSFrameHandler.java b/src/main/java/moe/ymc/acron/net/WSFrameHandler.java
index c578f2e..713d433 100644
--- a/src/main/java/moe/ymc/acron/net/WSFrameHandler.java
+++ b/src/main/java/moe/ymc/acron/net/WSFrameHandler.java
@@ -1,5 +1,6 @@
package moe.ymc.acron.net;
+import com.google.gson.JsonParseException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.*;
@@ -57,10 +58,18 @@ public class WSFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame>
final ClientIdentification identification = ctx.channel().attr(Attributes.ID).get();
final ClientConfiguration configuration = ctx.channel().attr(Attributes.CONFIGURATION).get();
- int id = -2;
+ int id;
+ final Request request;
try {
- final Request request = Serializer.deserialize(frame);
+ request = Serializer.deserialize(frame);
id = request.getId();
+ } catch (JsonParseException | IllegalArgumentException | IllegalStateException e) {
+ ctx.channel().writeAndFlush(
+ Serializer.serialize(new EventError(-2, EventError.Code.BAD_REQUEST.value, e.getMessage()))
+ );
+ return;
+ }
+ try {
if (request instanceof final ReqCmd reqCmd) {
LOGGER.info("Client {} executed a command: `{}`.",
identification.client().id(),
@@ -90,7 +99,9 @@ public class WSFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame>
"This may just be a malformed request. " +
"It is reported to the client.",
e);
- ctx.channel().writeAndFlush(Serializer.serialize(new EventError(id, e.getMessage())));
+ ctx.channel().writeAndFlush(
+ Serializer.serialize(new EventError(id, EventError.Code.SERVER_ERROR.value, e.getMessage()))
+ );
}
}