Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExceptionDecor |
|
| 2.0;2 |
1 | /** | |
2 | * Copyright (c) 2012-2015, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.log; | |
31 | ||
32 | import java.io.PrintWriter; | |
33 | import java.io.StringWriter; | |
34 | import java.util.Formattable; | |
35 | import java.util.FormattableFlags; | |
36 | import java.util.Formatter; | |
37 | import lombok.EqualsAndHashCode; | |
38 | import lombok.ToString; | |
39 | ||
40 | /** | |
41 | * Decorates an exception. | |
42 | * | |
43 | * <p>For example: | |
44 | * | |
45 | * <pre> | |
46 | * try { | |
47 | * // ... | |
48 | * } catch (final IOException ex) { | |
49 | * Logger.error("failed to open file: %[exception]s", ex); | |
50 | * throw new IllegalArgumentException(ex); | |
51 | * } | |
52 | * </pre> | |
53 | * | |
54 | * @author Yegor Bugayenko (yegor@teamed.io) | |
55 | * @version $Id: 56ec78353d55e0139ef76386f5e5cca4391304e7 $ | |
56 | * @since 0.1 | |
57 | */ | |
58 | 0 | @ToString |
59 | 0 | @EqualsAndHashCode(of = "throwable") |
60 | final class ExceptionDecor implements Formattable { | |
61 | ||
62 | /** | |
63 | * The exception. | |
64 | */ | |
65 | private final transient Throwable throwable; | |
66 | ||
67 | /** | |
68 | * Public ctor. | |
69 | * @param thr The exception | |
70 | */ | |
71 | 0 | ExceptionDecor(final Throwable thr) { |
72 | 0 | this.throwable = thr; |
73 | 0 | } |
74 | ||
75 | /** | |
76 | * {@inheritDoc} | |
77 | * @checkstyle ParameterNumber (4 lines) | |
78 | */ | |
79 | @Override | |
80 | public void formatTo(final Formatter formatter, final int flags, | |
81 | final int width, final int precision) { | |
82 | final String text; | |
83 | 0 | if (this.throwable == null) { |
84 | 0 | text = "NULL"; |
85 | 0 | } else if ((flags & FormattableFlags.ALTERNATE) == 0) { |
86 | 0 | final StringWriter writer = new StringWriter(); |
87 | 0 | this.throwable.printStackTrace(new PrintWriter(writer)); |
88 | 0 | text = writer.toString(); |
89 | 0 | } else { |
90 | 0 | text = this.throwable.getMessage(); |
91 | } | |
92 | 0 | formatter.format("%s", text); |
93 | 0 | } |
94 | ||
95 | } |