1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.melati.template.webmacro;
45
46 import org.melati.template.MarkupLanguage;
47
48 import org.webmacro.PropertyException;
49 import org.webmacro.Context;
50 import org.webmacro.Broker;
51 import org.webmacro.engine.EvaluationExceptionHandler;
52 import org.webmacro.engine.Variable;
53 import org.webmacro.util.Settings;
54
55
56
57
58
59 public class PassbackEvaluationExceptionHandler
60 implements EvaluationExceptionHandler {
61
62 @Override
63 public void init(Broker b, Settings config) {}
64
65 @Override
66 public void evaluate(Variable variable,
67 Context context,
68 Exception problem)
69 throws PropertyException {
70 if (problem instanceof PropertyException.NoSuchVariableException
71 || problem instanceof PropertyException.NullValueException
72 || problem instanceof PropertyException.NullToStringException)
73 return;
74 throw new PropertyException("Failed to evaluate " +
75 variable.getVariableName() + ": " + problem,problem);
76 }
77
78 @Override
79 public String expand(Variable variable,
80 Context context,
81 Exception problem)
82 throws PropertyException {
83 MarkupLanguage ml = (MarkupLanguage)context.get("ml");
84 if (ml == null) throw new PropertyException(
85 "Error, to use the Passback Evaluation Exception Handler, you must " +
86 "place your MarkupLanguage in the context as $ml" +
87 variable.getVariableName() + ": " + problem, problem);
88 Throwable underlying = problem;
89 if (problem instanceof PropertyException) {
90 PropertyException prob = (PropertyException)problem;
91 if (prob.getCause() != null) {
92 if (prob.getCause() instanceof PropertyException)
93 prob = (PropertyException)prob.getCause();
94 underlying = prob.getCause();
95 }
96 }
97 return ml.rendered(underlying);
98 }
99
100 @Override
101 public String warningString(String warningText) throws PropertyException {
102 throw new PropertyException("Evaluation warning: " + warningText);
103 }
104
105 @Override
106 public String errorString(String errorText) throws PropertyException {
107 throw new PropertyException("Evaluation error: " + errorText);
108 }
109 }
110