1 /*
2 * Copyright 2015 Data Archiving and Networked Services (an institute of
3 * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4 * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5 *
6 * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7 * the European Commission - subsequent versions of the EUPL (the "Licence");
8 * You may not use this work except in compliance with the Licence.
9 * You may obtain a copy of the Licence at:
10 *
11 * https://joinup.ec.europa.eu/software/page/eupl
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the Licence is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the Licence for the specific language governing
17 * permissions and limitations under the Licence.
18 */
19
20 package eu.ehri.project.exceptions;
21
22 import com.google.common.collect.ListMultimap;
23 import eu.ehri.project.persistence.Bundle;
24 import eu.ehri.project.persistence.ErrorSet;
25
26 /**
27 * Represents the failure of incoming data to confirm to
28 * some expected format. Like the {@link Bundle} class,
29 * errors are held as a sub-tree corresponding to the
30 * branches of the incoming data.
31 */
32 public class ValidationError extends Exception {
33
34 private static final long serialVersionUID = 1L;
35 private final ErrorSet errorSet;
36 private final Bundle bundle;
37
38 public ValidationError(Bundle bundle, ErrorSet errorSet) {
39 this.bundle = bundle;
40 this.errorSet = errorSet;
41 }
42
43 public ValidationError(Bundle bundle, String key, String error) {
44 this(bundle, ErrorSet.fromError(key, error));
45 }
46
47 public ValidationError(Bundle bundle, ListMultimap<String, String> errors) {
48 this(bundle, new ErrorSet(errors));
49 }
50
51 private static String formatErrors(String clsName, String id, ErrorSet errorSet) {
52 return String.format("%s %s: %s%n", clsName, id, errorSet.toString());
53 }
54
55 public String getMessage() {
56 return formatErrors(bundle.getType().getName(), bundle.getId(), errorSet);
57 }
58
59 public ErrorSet getErrorSet() {
60 return errorSet;
61 }
62
63 public Bundle getBundle() {
64 return bundle;
65 }
66 }