1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.util

File DefaultErrorBlockGenerator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

8
17
2
1
99
57
6
0.35
8.5
2
3

Classes

Class Line # Actions
DefaultErrorBlockGenerator 50 17 0% 6 4
0.851851985.2%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.util;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Inject;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang3.exception.ExceptionUtils;
32    import org.slf4j.Logger;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.rendering.block.Block;
35    import org.xwiki.rendering.block.FormatBlock;
36    import org.xwiki.rendering.block.GroupBlock;
37    import org.xwiki.rendering.block.VerbatimBlock;
38    import org.xwiki.rendering.block.WordBlock;
39    import org.xwiki.rendering.listener.Format;
40    import org.xwiki.rendering.util.ErrorBlockGenerator;
41   
42    /**
43    * Default implementation to generate error blocks to render an error in a wiki page.
44    *
45    * @version $Id: a8885f27341802299b922be57d7ff5e1019f1d04 $
46    * @since 8.1M1
47    */
48    @Component
49    @Singleton
 
50    public class DefaultErrorBlockGenerator implements ErrorBlockGenerator
51    {
52    @Inject
53    private Logger logger;
54   
 
55  275 toggle @Override
56    public List<Block> generateErrorBlocks(String message, String description, boolean isInline)
57    {
58  276 List<Block> errorBlocks = new ArrayList<>();
59   
60  276 Map<String, String> errorBlockParams =
61    Collections.singletonMap(CLASS_ATTRIBUTE_NAME, CLASS_ATTRIBUTE_MESSAGE_VALUE);
62  276 Map<String, String> errorDescriptionBlockParams =
63    Collections.singletonMap(CLASS_ATTRIBUTE_NAME, CLASS_ATTRIBUTE_DESCRIPTION_VALUE);
64   
65  276 Block descriptionBlock = new VerbatimBlock(description, isInline);
66   
67  276 if (isInline) {
68  125 errorBlocks.add(new FormatBlock(Arrays.asList(new WordBlock(message)), Format.NONE, errorBlockParams));
69  125 errorBlocks.add(new FormatBlock(Arrays.asList(descriptionBlock), Format.NONE, errorDescriptionBlockParams));
70    } else {
71  150 errorBlocks.add(new GroupBlock(Arrays.asList(new WordBlock(message)), errorBlockParams));
72  151 errorBlocks.add(new GroupBlock(Arrays.asList(descriptionBlock), errorDescriptionBlockParams));
73    }
74   
75  276 return errorBlocks;
76    }
77   
 
78  58 toggle @Override
79    public List<Block> generateErrorBlocks(String messagePrefix, Throwable throwable, boolean isInline)
80    {
81    // Note: We're using ExceptionUtils.getRootCause(e).getMessage() instead of getRootCauseMessage()
82    // below because getRootCauseMessage() adds a technical prefix (the name of the exception), that
83    // we don't want to display to our users.
84  58 Throwable rootCause = ExceptionUtils.getRootCause(throwable);
85  58 if (rootCause == null) {
86    // If there's no nested exception, fall back to the throwable itself for getting the cause
87  0 rootCause = throwable;
88    }
89   
90  58 String augmentedMessage = String.format("%s%s", messagePrefix,
91  58 rootCause == null ? "" : String.format(". Cause: [%s]", rootCause.getMessage()));
92  58 augmentedMessage = String.format("%s%sClick on this message for details.", augmentedMessage,
93  58 augmentedMessage.trim().endsWith(".") ? " " : ". ");
94   
95  58 this.logger.debug(augmentedMessage);
96   
97  58 return generateErrorBlocks(augmentedMessage, ExceptionUtils.getStackTrace(throwable), isInline);
98    }
99    }