View Javadoc
1   /**
2    * This file Copyright (c) 2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.config.source.yaml;
35  
36  import info.magnolia.resourceloader.Resource;
37  
38  import org.yaml.snakeyaml.error.Mark;
39  import org.yaml.snakeyaml.error.MarkedYAMLException;
40  import org.yaml.snakeyaml.error.YAMLException;
41  
42  /**
43   * Augments {@link YAMLException} with information about resource in which it has occurred.
44   */
45  public class YamlReaderException extends RuntimeException {
46  
47      public YamlReaderException(YAMLException cause, Resource relatedResource) {
48          super(formatYamlExceptionMessage(relatedResource, cause), cause);
49      }
50  
51      private static String formatYamlExceptionMessage(Resource res, YAMLException ye) {
52          final StringBuilder sb = new StringBuilder();
53          sb.append("YAML parsing error in ").append(res);
54          if (ye instanceof MarkedYAMLException) {
55              final Mark mark = ((MarkedYAMLException) ye).getProblemMark();
56              final String snippet = mark != null ? mark.get_snippet() : null;
57              final String problem = ((MarkedYAMLException) ye).getProblem();
58              if (mark != null) {
59                  sb.append(" at line ").append(mark.getLine()).append(", column ").append(mark.getColumn());
60              }
61              if (snippet != null) {
62                  sb.append(":\n");
63                  sb.append(snippet);
64              }
65              if (problem != null) {
66                  // TODO SnakeYaml tends to be a bit nasty about this "problem", e.g org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens() prints the actual char THEN it's representation, so you end up with a big fat actual tab in the message instead of JUST \t(TAB)
67                  // See https://code.google.com/p/snakeyaml/issues/detail?id=209
68                  sb.append(": ").append(problem.replaceAll("\\s*\\t\\s*", " "));
69              }
70          } else {
71              sb.append(": ").append(ye.getMessage());
72          }
73  
74          return sb.toString();
75      }
76  }