Skip to content

GatewayUtils does not detect http status code range #9

@tassun

Description

@tassun

I found that GatewayUtils class does not detect http status code come from error class attribute code thrown, please make a new release version changed please.

public static final void sendError(WebResponse rsp, Throwable cause) {
try {
MoleculerError error = null;
Throwable err = cause;
while (err != null) {
if (err instanceof MoleculerError) {
error = (MoleculerError) err;
break;
}
if (err.getCause() == null) {
break;
}
err = err.getCause();
}
if (error == null) {
String msg = null;
String type = null;
if (err != null) {
msg = err.getMessage();
type = err.getClass().getName();
int i = type.lastIndexOf('.');
if (i > -1) {
type = type.substring(i + 1);
}
type = type.replaceAll("(.)(\p{Upper})", "$1_$2").toUpperCase();
}
if (msg == null || msg.isEmpty()) {
msg = "Unknown error occured!";
}
if (type == null || type.isEmpty()) {
type = "MOLECULER_ERROR";
}
error = new MoleculerError(msg, cause, "unknown", false, 500, type, null);
}
Tree json = error.toTree();
byte[] body = json.toBinary();
int statusCode = error.getCode();
if (statusCode < 100 || statusCode > 599) {
statusCode = 500;
}
rsp.setStatus(statusCode);
rsp.setHeader(CACHE_CONTROL, NO_CACHE);
rsp.setHeader(CONTENT_TYPE, CONTENT_TYPE_JSON);
rsp.setHeader(CONTENT_LENGTH, Integer.toString(body.length));
rsp.send(body);
} catch (Exception ignored) {
logger.debug("Unable to send error response!", ignored);
} finally {
rsp.end();
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions