Skip to content

Commit 7671044

Browse files
committed
Implement unified message structure for all error messages
Signed-off-by: Emad Alblueshi <[email protected]>
1 parent 5862b72 commit 7671044

File tree

4 files changed

+82
-68
lines changed

4 files changed

+82
-68
lines changed

vertx-web/src/main/asciidoc/index.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,8 +2549,8 @@ follow the bridge wire protocol and look like this:
25492549
----
25502550
{
25512551
"type": "err",
2552-
"failureType": "socketException",
2553-
"message": "optionally a message from the exception being raised"
2552+
"failureType": "SOCKET_EXCEPTION",
2553+
"message": "A message from the exception being raised"
25542554
}
25552555
----
25562556

vertx-web/src/main/java/io/vertx/ext/web/handler/sockjs/impl/EventBusBridgeImpl.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ private void handleSocketData(SockJSSocket sock, Buffer data, Map<String, Messag
8080
try {
8181
msg = new JsonObject(data.toString());
8282
} catch (DecodeException e) {
83-
replyError(sock, "invalid_json");
83+
replyError(sock, "INVALID_JSON", "Malformed JSON");
8484
return;
8585
}
8686

8787
String type = msg.getString("type");
8888
if (type == null) {
89-
replyError(sock, "missing_type");
89+
replyError(sock, "MISSING_TYPE", "Message type is missing");
9090
return;
9191
}
9292

@@ -95,7 +95,7 @@ private void handleSocketData(SockJSSocket sock, Buffer data, Map<String, Messag
9595
} else {
9696
String address = msg.getString("address");
9797
if (address == null) {
98-
replyError(sock, "missing_address");
98+
replyError(sock, "MISSING_ADDRESS", "Message address is missing");
9999
return;
100100
}
101101
switch (type) {
@@ -113,7 +113,7 @@ private void handleSocketData(SockJSSocket sock, Buffer data, Map<String, Messag
113113
break;
114114
default:
115115
LOG.error("Invalid type in incoming message: " + type);
116-
replyError(sock, "invalid_type");
116+
replyError(sock, "INVALID_TYPE", "Invalid message type");
117117
}
118118
}
119119

@@ -165,19 +165,19 @@ private void internalHandleSendOrPub(SockJSSocket sock, boolean send, JsonObject
165165
() -> {
166166
String address = msg.getString("address");
167167
if (address == null) {
168-
replyError(sock, "missing_address");
168+
replyError(sock, "MISSING_ADDRESS", "Message address is missing");
169169
return;
170170
}
171171
doSendOrPub(send, sock, address, msg);
172-
}, () -> replyError(sock, "rejected"));
172+
}, () -> replyError(sock, "REJECTED", "Message is rejected"));
173173
}
174174

175175
private boolean checkMaxHandlers(SockJSSocket sock, SockInfo info) {
176176
if (info.handlerCount < maxHandlersPerSocket) {
177177
return true;
178178
} else {
179179
LOG.warn("Refusing to register as max_handlers_per_socket reached already");
180-
replyError(sock, "max_handlers_reached");
180+
replyError(sock, "HANDLERS_MAX_LIMIT", "Registration handlers exceed the maximum limit");
181181
return false;
182182
}
183183
}
@@ -192,11 +192,11 @@ private void internalHandleRegister(SockJSSocket sock, JsonObject rawMsg, Map<St
192192
final boolean debug = LOG.isDebugEnabled();
193193
final String address = rawMsg.getString("address");
194194
if (address == null) {
195-
replyError(sock, "missing_address");
195+
replyError(sock, "MISSING_ADDRESS", "Message address is missing");
196196
return;
197197
} else if (address.length() > maxAddressLength) {
198198
LOG.warn("Refusing to register as address length > max_address_length");
199-
replyError(sock, "max_address_length_reached");
199+
replyError(sock, "ADDRESS_MAX_LENGTH", "Address exceeds maximum length");
200200
return;
201201
}
202202
Match match = checkMatches(false, address, null);
@@ -206,7 +206,7 @@ private void internalHandleRegister(SockJSSocket sock, JsonObject rawMsg, Map<St
206206
// loop could DDoS the bridge.
207207
if (registrations.containsKey(address)) {
208208
LOG.warn("Refusing to register as address is already registered");
209-
replyError(sock, "address_already_registered");
209+
replyError(sock, "ADDRESS_ALREADY_REGISTERED", "Address is already registered");
210210
return;
211211
}
212212

@@ -247,25 +247,25 @@ private void internalHandleRegister(SockJSSocket sock, JsonObject rawMsg, Map<St
247247
checkCallHook(() -> new BridgeEventImpl(BridgeEventType.REGISTERED, rawMsg, sock));
248248
} else {
249249
LOG.warn("Cannot register handler for address " + address, ar.cause());
250-
replyError(sock, "registration_failure");
250+
replyError(sock, "ADDRESS_REGISTRATION", "Address registration is failed");
251251
}
252252
});
253253
} else {
254254
// inbound match failed
255255
if (debug) {
256256
LOG.debug("Cannot register handler for address " + address + " because there is no inbound match");
257257
}
258-
replyError(sock, "access_denied");
258+
replyError(sock, "ACCESS_DENIED", "Address access is denied");
259259
}
260-
}, () -> replyError(sock, "rejected"));
260+
}, () -> replyError(sock, "REJECTED", "Message is rejected"));
261261
}
262262

263263
private void internalHandleUnregister(SockJSSocket sock, JsonObject rawMsg, Map<String, MessageConsumer<?>> registrations) {
264264
checkCallHook(() -> new BridgeEventImpl(BridgeEventType.UNREGISTER, rawMsg, sock),
265265
() -> {
266266
String address = rawMsg.getString("address");
267267
if (address == null) {
268-
replyError(sock, "missing_address");
268+
replyError(sock, "MISSING_ADDRESS","Message address is missing");
269269
return;
270270
}
271271
Match match = checkMatches(false, address, null);
@@ -280,9 +280,9 @@ private void internalHandleUnregister(SockJSSocket sock, JsonObject rawMsg, Map<
280280
if (LOG.isDebugEnabled()) {
281281
LOG.debug("Cannot unregister handler for address " + address + " because there is no inbound match");
282282
}
283-
replyError(sock, "access_denied");
283+
replyError(sock, "ACCESS_DENIED", "Address access is denied");
284284
}
285-
}, () -> replyError(sock, "rejected"));
285+
}, () -> replyError(sock, "REJECTED", "Message is rejected"));
286286
}
287287

288288
private void internalHandlePing(final SockJSSocket sock) {
@@ -317,7 +317,7 @@ public void handle(final SockJSSocket sock) {
317317
checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_IDLE, null, sock),
318318
// We didn't receive a ping in time so close the socket
319319
((SockJSSocketBase) sock)::closeAfterSessionExpired,
320-
() -> replyError(sock, "rejected"));
320+
() -> replyError(sock, "REJECTED", "Message is rejected"));
321321
}
322322
});
323323
SockInfo sockInfo = new SockInfo();
@@ -334,9 +334,11 @@ private void handleSocketClosed(SockJSSocket sock, Map<String, MessageConsumer<?
334334
private void handleSocketException(SockJSSocket sock, Throwable err, Map<String, MessageConsumer<?>> registrations) {
335335
LOG.error("SockJSSocket exception", err);
336336
clearSocketState(sock, registrations);
337-
final JsonObject msg = new JsonObject().put("type", "err").put("failureType", "socketException");
337+
final JsonObject msg = new JsonObject().put("type", "err").put("failureCode", -1).put("failureType", "SOCKET_EXCEPTION");
338338
if (err != null) {
339339
msg.put("message", err.getMessage());
340+
} else {
341+
msg.put("message", "A socket exception occurred while attempting to establish or maintain a network connection");
340342
}
341343
checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_ERROR, msg, sock));
342344
}
@@ -408,7 +410,7 @@ private void doSendOrPub(boolean send, SockJSSocket sock, String address,
408410
if (replyAddress != null && replyAddress.length() > 36) {
409411
// vertx-eventbus.js ids are always 36 chars
410412
LOG.error("Will not send message, reply address is > 36 chars");
411-
replyError(sock, "invalid_reply_address");
413+
replyError(sock, "INVALID_REPLY_ADDRESS", "Reply address is invalid");
412414
return;
413415
}
414416
final boolean debug = LOG.isDebugEnabled();
@@ -431,19 +433,19 @@ private void doSendOrPub(boolean send, SockJSSocket sock, String address,
431433
if (ok) {
432434
checkAndSend(send, address, body, headers, sock, replyAddress, awaitingReply);
433435
} else {
434-
replyError(sock, "access_denied");
436+
replyError(sock, "ACCESS_DENIED", "Address access is denied");
435437
if (debug) {
436438
LOG.debug("Inbound message for address " + address + " rejected because is not authorised");
437439
}
438440
}
439441
})
440442
.onFailure(err -> {
441-
replyError(sock, "auth_error");
443+
replyError(sock, "AUTHZ", "Authorization failed");
442444
LOG.error("Error in performing authorization", err);
443445
});
444446
} else {
445447
// no web session
446-
replyError(sock, "not_logged_in");
448+
replyError(sock, "AUTHN", "Authentication is required");
447449
if (debug) {
448450
LOG.debug("Inbound message for address " + address +
449451
" rejected because it requires auth and user is not authenticated");
@@ -454,7 +456,7 @@ private void doSendOrPub(boolean send, SockJSSocket sock, String address,
454456
}
455457
} else {
456458
// inbound match failed
457-
replyError(sock, "access_denied");
459+
replyError(sock, "ACCESS_DENIED", "Address access is denied");
458460
if (debug) {
459461
LOG.debug("Inbound message for address " + address + " rejected because there is no match");
460462
}
@@ -590,8 +592,12 @@ private boolean regexMatches(String matchRegex, String address) {
590592
return m.matches();
591593
}
592594

593-
private static void replyError(SockJSSocket sock, String err) {
594-
JsonObject envelope = new JsonObject().put("type", "err").put("body", err);
595+
private static void replyError(SockJSSocket sock, String type, String message) {
596+
JsonObject envelope = new JsonObject()
597+
.put("type", "err")
598+
.put("failureCode", -1)
599+
.put("failureType", type)
600+
.put("message", message);
595601
sock.write(buffer(envelope.encode()));
596602
}
597603

0 commit comments

Comments
 (0)