/** * Returns the message contained in {@code file}. Throws an unchecked exception * if the file does not exist, is empty or does not contain message with the * expected type. */ public static <MSG extends Message> MSG read(File file, Parser<MSG> parser) { InputStream input = null; try { input = new BufferedInputStream(new FileInputStream(file)); return parser.parseFrom(input); } catch (Exception e) { throw ContextException.of("Unable to read message", e).addContext("file", file); } finally { IOUtils.closeQuietly(input); } }
@Test public void merge_ContextException() { ContextException cause = ContextException.of("cause").addContext("K1", "V1"); ContextException e = ContextException.of(cause) .addContext("K1", "V11") .addContext("K2", "V2"); assertThat(e.getContext("K1")).containsExactly("V1", "V11"); assertThat(e.getContext("K2")).containsOnly("V2"); assertThat(e).hasMessage("K1=[V1,V11] | K2=V2"); }
public static ContextException of(String message, Throwable t) { if (t instanceof ContextException) { return new ContextException(message, t.getCause()).addContext((ContextException) t); } return new ContextException(message, t); }
public ContextException setContext(String key, @Nullable Object value) { clearContext(key); return addContext(key, value); }
@Test public void setContext() { ContextException e = ContextException.of(LABEL) .addContext("K1", "V1") .setContext("K1", "V2"); assertThat(e).hasMessage(LABEL + " | K1=V2"); }
@Test public void only_label() { ContextException e = ContextException.of(LABEL); assertThat(e.getMessage()).isEqualTo(LABEL); assertThat(e.getRawMessage()).isEqualTo(LABEL); assertThat(e.getCause()).isNull(); }
public static <MSG extends Message> MSG read(InputStream input, Parser<MSG> parser) { try { return parser.parseFrom(input); } catch (Exception e) { throw ContextException.of("Unable to read message", e); } finally { IOUtils.closeQuietly(input); } }
private String format(@Nullable String baseMessage) { StringBuilder sb = new StringBuilder(); Iterator<String> keyIt = context.keySet().iterator(); if (StringUtils.isNotBlank(baseMessage)) { sb.append(baseMessage); if (keyIt.hasNext()) { sb.append(" | "); } } while (keyIt.hasNext()) { String key = keyIt.next(); sb.append(key).append("="); List<Object> values = getContext(key); if (values.size() > 1) { sb.append("[").append(COMMA_JOINER.join(values)).append("]"); } else if (values.size() == 1) { sb.append(values.get(0)); } if (keyIt.hasNext()) { sb.append(" | "); } } return sb.toString(); } }
@Override @Nonnull public String getMessage() { return format(super.getMessage()); }
public static ContextException of(String message) { return new ContextException(message); }
@Test public void only_cause() { Exception cause = new Exception("cause"); ContextException e = ContextException.of(cause); assertThat(e.getMessage()).isEqualTo("java.lang.Exception: cause"); assertThat(e.getRawMessage()).isEqualTo("java.lang.Exception: cause"); assertThat(e.getCause()).isSameAs(cause); }
@Override protected MSG doNext() { try { return parser.parsePartialDelimitedFrom(input); } catch (InvalidProtocolBufferException e) { throw ContextException.of(e); } }
public ContextException setContext(String key, @Nullable Object value) { clearContext(key); return addContext(key, value); }
private String format(@Nullable String baseMessage) { StringBuilder sb = new StringBuilder(); Iterator<String> keyIt = context.keySet().iterator(); if (StringUtils.isNotBlank(baseMessage)) { sb.append(baseMessage); if (keyIt.hasNext()) { sb.append(" | "); } } while (keyIt.hasNext()) { String key = keyIt.next(); sb.append(key).append("="); List<Object> values = getContext(key); if (values.size() > 1) { sb.append("[").append(COMMA_JOINER.join(values)).append("]"); } else if (values.size() == 1) { sb.append(values.get(0)); } if (keyIt.hasNext()) { sb.append(" | "); } } return sb.toString(); } }
@Override @Nonnull public String getMessage() { return format(super.getMessage()); }
public static ContextException of(String message) { return new ContextException(message); }
/** * Reads a stream of messages. This method returns an empty iterator if there are no messages. An * exception is raised on IO error, if file does not exist or if messages have a * different type than {@code parser}. */ public static <MSG extends Message> CloseableIterator<MSG> readStream(File file, Parser<MSG> parser) { try { // the input stream is closed by the CloseableIterator BufferedInputStream input = new BufferedInputStream(new FileInputStream(file)); return readStream(input, parser); } catch (Exception e) { throw ContextException.of("Unable to read messages", e).addContext("file", file); } }
@Test public void merge_ContextException_with_new_message() { ContextException cause = ContextException.of("cause").addContext("K1", "V1"); ContextException e = ContextException.of(LABEL, cause).addContext("K2", "V2"); assertThat(e.getContext("K1")).containsOnly("V1"); assertThat(e.getContext("K2")).containsOnly("V2"); assertThat(e).hasMessage(LABEL + " | K1=V1 | K2=V2"); } }
public static ContextException of(Throwable t) { if (t instanceof ContextException) { return new ContextException(t.getCause()).addContext((ContextException) t); } return new ContextException(t); }
@Test public void cause_and_message() { Exception cause = new Exception("cause"); ContextException e = ContextException.of(LABEL, cause); assertThat(e.getMessage()).isEqualTo(LABEL); assertThat(e.getRawMessage()).isEqualTo(LABEL); assertThat(e.getCause()).isSameAs(cause); }