@Override public FailureItem build() { return new FailureItem( reason, message, attributes, stackTrace, causeType); }
/** * Returns an instance with the specified attributes added. * <p> * If the attribute map of this instance has any of the new attribute keys, the values are replaced. * * @param attributes the new attributes to add * @return the new failure item */ public FailureItem withAttributes(Map<String, String> attributes) { Map<String, String> newAttributes = new HashMap<>(this.attributes); newAttributes.putAll(attributes); return new FailureItem(reason, message, newAttributes, stackTrace, causeType); }
/** * Returns an instance with the specified attribute added. * <p> * If the attribute map of this instance has the specified key, the value is replaced. * * @param key the key to add * @param value the value to add * @return the new failure item */ public FailureItem withAttribute(String key, String value) { Map<String, String> attributes = new HashMap<>(this.attributes); attributes.put(key, value); return new FailureItem(reason, message, attributes, stackTrace, causeType); }
/** * Obtains a failure from a reason and message. * <p> * The failure will still have a stack trace, but the cause type will not be present. * * @param reason the reason * @param message the failure message, not empty * @param skipFrames the number of caller frames to skip, not including this one * @return the failure */ static FailureItem of(FailureReason reason, String message, int skipFrames) { ArgChecker.notNull(reason, "reason"); ArgChecker.notEmpty(message, "message"); String stackTrace = localGetStackTraceAsString(message, skipFrames); return new FailureItem(reason, message, ImmutableMap.of(), stackTrace, null); }
/** * Obtains a failure from a reason and message. * <p> * The failure will still have a stack trace, but the cause type will not be present. * * @param reason the reason * @param message the failure message, not empty * @param attributes the attributes associated with this failure * @return the failure */ private static FailureItem of(FailureReason reason, String message, Map<String, String> attributes) { ArgChecker.notNull(reason, "reason"); ArgChecker.notEmpty(message, "message"); String stackTrace = localGetStackTraceAsString(message, 1); return new FailureItem(reason, message, attributes, stackTrace, null); }
/** * Obtains a failure from a reason, throwable and message. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#formatWithAttributes(String, Object...)} for more details. * * @param reason the reason * @param cause the cause * @param message a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return the failure */ public static FailureItem of(FailureReason reason, Throwable cause, String message, Object... messageArgs) { ArgChecker.notNull(reason, "reason"); ArgChecker.notNull(cause, "cause"); Pair<String, Map<String, String>> msg = Messages.formatWithAttributes(message, messageArgs); String stackTrace = Throwables.getStackTraceAsString(cause).replace(System.lineSeparator(), "\n"); FailureItem base = new FailureItem(reason, msg.getFirst(), msg.getSecond(), stackTrace, cause.getClass()); String causeMessage = cause.getMessage(); if (!base.getAttributes().containsKey(EXCEPTION_MESSAGE_ATTRIBUTE) && !Strings.isNullOrEmpty(causeMessage)) { return base.withAttribute(EXCEPTION_MESSAGE_ATTRIBUTE, causeMessage); } return base; }