Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / SourceException.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.source;
9
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12
13 /**
14  *
15  * Thrown to indicate error in YANG model source.
16  *
17  */
18 public class SourceException extends RuntimeException {
19
20     private static final long serialVersionUID = 1L;
21
22     private final StatementSourceReference sourceRef;
23
24     public SourceException(@Nonnull String message,@Nonnull StatementSourceReference source) {
25         super(createMessage(message, source));
26         sourceRef = source;
27     }
28
29     public SourceException(@Nonnull String message,@Nonnull StatementSourceReference source, Throwable cause) {
30         super(createMessage(message, source), cause);
31         sourceRef = source;
32     }
33
34     public @Nonnull StatementSourceReference getSourceReference() {
35         return sourceRef;
36     }
37
38     private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
39         Preconditions.checkNotNull(message);
40         Preconditions.checkNotNull(source);
41
42         return String.format("%s\nStatement source at %s", message, source);
43     }
44
45 }