All files / src/Ice LoggerMiddleware.js

81.53% Statements 106/130
60.71% Branches 17/28
100% Functions 5/5
81.53% Lines 106/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 13141x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 803x 803x 790x 801x 803x 786x 470x 470x 786x 803x 803x 803x 803x       803x 803x 4x     4x 803x 790x 803x 13x       13x       13x 13x 2x 2x 13x     13x 13x 803x 41x 41x 12x 12x 12x 12x 12x 12x 12x 12x 12x 41x 41x 470x 470x 470x 470x 470x 470x 470x 470x 470x 41x 41x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 41x 41x 472x 472x 472x 472x 472x         472x 472x 472x 472x 472x 472x 472x 472x     472x     472x 472x 472x 472x 472x 472x 472x 472x 472x 472x       472x  
// Copyright (c) ZeroC, Inc.
 
import { UserException } from "./UserException.js";
import { DispatchException } from "./LocalExceptions.js";
import { UnknownException } from "./LocalExceptions.js";
import { IPConnectionInfo } from "./Connection.js";
import { identityToString } from "./IdentityToString.js";
import { StringUtil } from "./StringUtil.js";
import { Ice as Ice_ReplyStatus } from "./ReplyStatus.js";
const { ReplyStatus } = Ice_ReplyStatus;
 
export class LoggerMiddleware {
    async dispatch(request) {
        try {
            const response = await this._next.dispatch(request);
            switch (response.replyStatus) {
                case ReplyStatus.Ok:
                case ReplyStatus.UserException:
                    if (this._traceLevel > 0) {
                        this.logDispatch(response.replyStatus, request.current);
                    }
                    break;
 
                case ReplyStatus.UnknownException:
                case ReplyStatus.UnknownUserException:
                case ReplyStatus.UnknownLocalException:
                    // Always log when middleware installed.
                    this.logDispatchFailed(response.exceptionDetails, request.current);
                    break;
 
                default:
                    if (this._traceLevel > 0 || this._warningLevel > 1) {
                        this.logDispatchFailed(response.exceptionDetails, request.current);
                    }
                    break;
            }
            return response;
        } catch (ex) {
            if (ex instanceof UserException) {
                if (this._traceLevel > 0) {
                    this.logDispatch(ReplyStatus.UserException, request.current);
                }
            } else if (ex instanceof UnknownException) {
                // Always log when middleware installed.
                // The stack includes the class name and error message.
                this.logDispatchFailed(ex.stack, request.current);
            } else if (ex instanceof DispatchException) {
                if (this._traceLevel > 0 || this._warningLevel > 1) {
                    this.logDispatchFailed(ex.stack, request.current);
                }
            } else {
                this.logDispatchFailed(ex.stack, request.current);
            }
            throw ex;
        }
    }
 
    constructor(next, logger, traceLevel, traceCat, warningLevel, toStringMode) {
        this._next = next;
        this._logger = logger;
        this._traceLevel = traceLevel;
        this._traceCat = traceCat;
        this._warningLevel = warningLevel;
        this._toStringMode = toStringMode;
 
        console.assert(traceLevel > 0 || warningLevel > 0);
    }
 
    logDispatch(replyStatus, current) {
        const output = [];
        output.push("dispatch of ");
        output.push(current.operation);
        output.push(" to ");
        printTarget(output, this._toStringMode, current);
        output.push(" returned a response with reply status ");
        output.push(replyStatus);
        this._logger.trace(this._traceCat, output.join(""));
    }
 
    logDispatchFailed(exceptionDetails, current) {
        const output = [];
        output.push("failed to dispatch ");
        output.push(current.operation);
        output.push(" to ");
        printTarget(output, this._toStringMode, current);
 
        if (exceptionDetails !== null) {
            output.push("\n");
            output.push(exceptionDetails);
        }
        this._logger.warning(output.join(""));
    }
}
 
function printTarget(output, toStringMode, current) {
    output.push("'");
    output.push(identityToString(current.id, toStringMode));
    output.push("'");
    if (current.facet !== "") {
        output.push(" -f '");
        output.push(StringUtil.escapeString(current.facet, "", toStringMode));
        output.push("'");
    }
    output.push(" over ");
 
    console.assert(current.con !== null, "current.con is null"); // no colloc in JS
 
    let connInfo = null;
    try {
        connInfo = current.con.getInfo();
        while (connInfo.underlying !== null) {
            connInfo = connInfo.underlying;
        }
    } catch {
        // Thrown by getInfo() when the connection is closed.
    }
 
    if (connInfo instanceof IPConnectionInfo) {
        output.push(connInfo.localAddress);
        output.push(":");
        output.push(connInfo.localPort);
        output.push("<->");
        output.push(connInfo.remoteAddress);
        output.push(":");
        output.push(connInfo.remotePort);
    } else {
        // Connection.toString() returns a multiline string, so we just use type here for bt and similar.
        output.push(current.con.type());
    }
}