Skip to content

Commit 0b389c5

Browse files
committed
[feature] Improve error logging when a Trigger event raises an exception
1 parent 1095fe2 commit 0b389c5

File tree

3 files changed

+248
-132
lines changed

3 files changed

+248
-132
lines changed

exist-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,9 @@
846846
<include>src/main/java/org/exist/collections/MutableCollection.java</include>
847847
<include>src/test/java/org/exist/collections/OpenCollectionTest</include>
848848
<include>src/main/java/org/exist/collections/triggers/CollectionTrigger.java</include>
849+
<include>src/main/java/org/exist/collections/triggers/CollectionTriggers.java</include>
849850
<include>src/main/java/org/exist/collections/triggers/DocumentTrigger.java</include>
851+
<include>src/main/java/org/exist/collections/triggers/DocumentTriggers.java</include>
850852
<include>src/test/java/org/exist/collections/triggers/MessagesTrigger.java</include>
851853
<include>src/test/java/org/exist/collections/triggers/TriggerConfigTest.java</include>
852854
<include>src/main/java/org/exist/collections/triggers/XQueryStartupTrigger.java</include>
@@ -1428,7 +1430,9 @@
14281430
<exclude>src/main/java/org/exist/collections/MutableCollection.java</exclude>
14291431
<exclude>src/test/java/org/exist/collections/OpenCollectionTest</exclude>
14301432
<exclude>src/main/java/org/exist/collections/triggers/CollectionTrigger.java</exclude>
1433+
<exclude>src/main/java/org/exist/collections/triggers/CollectionTriggers.java</exclude>
14311434
<exclude>src/main/java/org/exist/collections/triggers/DocumentTrigger.java</exclude>
1435+
<exclude>src/main/java/org/exist/collections/triggers/DocumentTriggers.java</exclude>
14321436
<exclude>src/test/java/org/exist/collections/triggers/MessagesTrigger.java</exclude>
14331437
<exclude>src/test/java/org/exist/collections/triggers/TriggerConfigTest.java</exclude>
14341438
<exclude>src/main/java/org/exist/collections/triggers/XQueryStartupTrigger.java</exclude>
Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -39,119 +63,146 @@ public class CollectionTriggers implements CollectionTrigger {
3963

4064
private final List<CollectionTrigger> triggers;
4165

42-
public CollectionTriggers(DBBroker broker, Txn transaction) throws TriggerException {
66+
public CollectionTriggers(final DBBroker broker, final Txn transaction) throws TriggerException {
4367
this(broker, transaction, null, null);
4468
}
4569

46-
public CollectionTriggers(DBBroker broker, Txn transaction, Collection collection) throws TriggerException {
70+
public CollectionTriggers(final DBBroker broker, final Txn transaction, final Collection collection) throws TriggerException {
4771
this(broker, transaction, collection, collection.getConfiguration(broker));
4872
}
4973

50-
public CollectionTriggers(DBBroker broker, Txn transaction, Collection collection, CollectionConfiguration config) throws TriggerException {
51-
52-
List<TriggerProxy<? extends CollectionTrigger>> colTriggers = null;
53-
if (config != null) {
54-
colTriggers = config.collectionTriggers();
55-
}
56-
57-
java.util.Collection<TriggerProxy<? extends CollectionTrigger>> masterTriggers = broker.getDatabase().getCollectionTriggers();
74+
public CollectionTriggers(final DBBroker broker, final Txn transaction, final Collection collection, final CollectionConfiguration config) throws TriggerException {
75+
final List<TriggerProxy<? extends CollectionTrigger>> colTriggers = config != null ? config.collectionTriggers() : null;
76+
final java.util.Collection<TriggerProxy<? extends CollectionTrigger>> masterTriggers = broker.getDatabase().getCollectionTriggers();
5877

5978
triggers = new ArrayList<>(masterTriggers.size() + (colTriggers == null ? 0 : colTriggers.size()));
6079

61-
for (TriggerProxy<? extends CollectionTrigger> colTrigger : masterTriggers) {
62-
63-
CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection);
64-
80+
for (final TriggerProxy<? extends CollectionTrigger> colTrigger : masterTriggers) {
81+
final CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection);
6582
register(instance);
6683
}
6784

6885
if (colTriggers != null) {
69-
for (TriggerProxy<? extends CollectionTrigger> colTrigger : colTriggers) {
70-
71-
CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection);
72-
86+
for (final TriggerProxy<? extends CollectionTrigger> colTrigger : colTriggers) {
87+
final CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection);
7388
register(instance);
7489
}
7590
}
7691
}
7792

78-
private void register(CollectionTrigger trigger) {
93+
private void register(final CollectionTrigger trigger) {
7994
triggers.add(trigger);
8095
}
8196

8297
@Override
83-
public void configure(DBBroker broker, Txn transaction, Collection col, Map<String, List<? extends Object>> parameters) throws TriggerException {
98+
public void configure(final DBBroker broker, final Txn transaction, final Collection col, final Map<String, List<? extends Object>> parameters) throws TriggerException {
8499
}
85100

86101
@Override
87-
public void beforeCreateCollection(DBBroker broker, Txn txn, XmldbURI uri) throws TriggerException {
88-
for (CollectionTrigger trigger : triggers) {
89-
trigger.beforeCreateCollection(broker, txn, uri);
102+
public void beforeCreateCollection(final DBBroker broker, final Txn txn, final XmldbURI uri) throws TriggerException {
103+
for (final CollectionTrigger trigger : triggers) {
104+
try {
105+
trigger.beforeCreateCollection(broker, txn, uri);
106+
} catch (final Exception e) {
107+
logAndThrowError("beforeCreateCollection", trigger, uri, e);
108+
}
90109
}
91110
}
92111

93112
@Override
94-
public void afterCreateCollection(DBBroker broker, Txn txn, Collection collection) {
95-
for (CollectionTrigger trigger : triggers) {
113+
public void afterCreateCollection(final DBBroker broker, final Txn txn, final Collection collection) {
114+
for (final CollectionTrigger trigger : triggers) {
96115
try {
97116
trigger.afterCreateCollection(broker, txn, collection);
98-
} catch (Exception e) {
99-
Trigger.LOG.error(e.getMessage(), e);
117+
} catch (final Exception e) {
118+
logError("afterCreateCollection", trigger, collection.getURI(), e);
100119
}
101120
}
102121
}
103122

104123
@Override
105-
public void beforeCopyCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI newUri) throws TriggerException {
106-
for (CollectionTrigger trigger : triggers) {
107-
trigger.beforeCopyCollection(broker, txn, collection, newUri);
124+
public void beforeCopyCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI newUri) throws TriggerException {
125+
for (final CollectionTrigger trigger : triggers) {
126+
try {
127+
trigger.beforeCopyCollection(broker, txn, collection, newUri);
128+
} catch (final Exception e) {
129+
logAndThrowError("beforeCopyCollection", trigger, collection.getURI(), e);
130+
}
108131
}
109132
}
110133

111134
@Override
112-
public void afterCopyCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI oldUri) {
113-
for (CollectionTrigger trigger : triggers) {
135+
public void afterCopyCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI oldUri) {
136+
for (final CollectionTrigger trigger : triggers) {
114137
try {
115138
trigger.afterCopyCollection(broker, txn, collection, oldUri);
116-
} catch (Exception e) {
117-
Trigger.LOG.error(e.getMessage(), e);
139+
} catch (final Exception e) {
140+
logError("afterCopyCollection", trigger, oldUri, e);
118141
}
119142
}
120143
}
121144

122145
@Override
123-
public void beforeMoveCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI newUri) throws TriggerException {
124-
for (CollectionTrigger trigger : triggers) {
125-
trigger.beforeMoveCollection(broker, txn, collection, newUri);
146+
public void beforeMoveCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI newUri) throws TriggerException {
147+
for (final CollectionTrigger trigger : triggers) {
148+
try {
149+
trigger.beforeMoveCollection(broker, txn, collection, newUri);
150+
} catch (final Exception e) {
151+
logAndThrowError("beforeMoveCollection", trigger, collection.getURI(), e);
152+
}
126153
}
127154
}
128155

129156
@Override
130-
public void afterMoveCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI oldUri) {
131-
for (CollectionTrigger trigger : triggers) {
157+
public void afterMoveCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI oldUri) {
158+
for (final CollectionTrigger trigger : triggers) {
132159
try {
133160
trigger.afterMoveCollection(broker, txn, collection, oldUri);
134-
} catch (Exception e) {
135-
Trigger.LOG.error(e.getMessage(), e);
161+
} catch (final Exception e) {
162+
logError("afterMoveCollection", trigger, oldUri, e);
136163
}
137164
}
138165
}
139166

140167
@Override
141-
public void beforeDeleteCollection(DBBroker broker, Txn txn, Collection collection) throws TriggerException {
142-
for (CollectionTrigger trigger : triggers) {
143-
trigger.beforeDeleteCollection(broker, txn, collection);
168+
public void beforeDeleteCollection(final DBBroker broker, final Txn txn, final Collection collection) throws TriggerException {
169+
for (final CollectionTrigger trigger : triggers) {
170+
try {
171+
trigger.beforeDeleteCollection(broker, txn, collection);
172+
} catch (final Exception e) {
173+
logAndThrowError("beforeDeleteCollection", trigger, collection.getURI(), e);
174+
}
144175
}
145176
}
146177

147178
@Override
148-
public void afterDeleteCollection(DBBroker broker, Txn txn, XmldbURI uri) {
149-
for (CollectionTrigger trigger : triggers) {
179+
public void afterDeleteCollection(final DBBroker broker, final Txn txn, final XmldbURI uri) {
180+
for (final CollectionTrigger trigger : triggers) {
150181
try {
151182
trigger.afterDeleteCollection(broker, txn, uri);
152-
} catch (Exception e) {
153-
Trigger.LOG.error(e.getMessage(), e);
183+
} catch (final Exception e) {
184+
logError("afterDeleteCollection", trigger, uri, e);
154185
}
155186
}
156187
}
188+
189+
private void logAndThrowError(final String eventName, final CollectionTrigger collectionTrigger, final XmldbURI source, final Exception e) throws TriggerException {
190+
logError(eventName, collectionTrigger, source, e);
191+
throwError(e);
192+
}
193+
194+
private void logError(final String eventName, final CollectionTrigger collectionTrigger, final XmldbURI source, final Exception e) {
195+
final String message = String.format("Error in %s#%s triggered by: %s, %s", collectionTrigger.getClass().getSimpleName(), eventName, source, e.getMessage());
196+
Trigger.LOG.error(message, e);
197+
}
198+
199+
private void throwError(final Exception e) throws TriggerException {
200+
if (e instanceof TriggerException) {
201+
throw (TriggerException) e;
202+
} else if (e instanceof RuntimeException) {
203+
throw (RuntimeException) e;
204+
} else {
205+
throw new TriggerException(e);
206+
}
207+
}
157208
}

0 commit comments

Comments
 (0)