| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.s9api; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.om.StructuredQName; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | |
| 14 | /** |
| 15 | * An exception thrown by the Saxon s9api API. This is always a wrapper for some other underlying exception |
| 16 | */ |
| 17 | public class SaxonApiException extends Exception { |
| 18 | |
| 19 | /** |
| 20 | * Create a SaxonApiException |
| 21 | * |
| 22 | * @param cause the underlying cause of the exception |
| 23 | */ |
| 24 | |
| 25 | public SaxonApiException(Throwable cause) { |
| 26 | super(cause); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Create a SaxonApiException |
| 31 | * |
| 32 | * @param message the message |
| 33 | */ |
| 34 | |
| 35 | public SaxonApiException(String message) { |
| 36 | super(new XPathException(message)); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create a SaxonApiException |
| 41 | * |
| 42 | * @param message the message |
| 43 | * @param cause the underlying cause of the exception |
| 44 | */ |
| 45 | |
| 46 | public SaxonApiException(String message, Throwable cause) { |
| 47 | super(new XPathException(message, cause)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the detail message string of this throwable. |
| 52 | * |
| 53 | * @return the detail message string of this <tt>Throwable</tt> instance |
| 54 | * (which may be <tt>null</tt>). |
| 55 | */ |
| 56 | public String getMessage() { |
| 57 | return getCause().getMessage(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the error code associated with the exception, if there is one |
| 62 | * |
| 63 | * @return the associated error code, or null if no error code is available |
| 64 | * @since 9.3 |
| 65 | */ |
| 66 | |
| 67 | /*@Nullable*/ |
| 68 | public QName getErrorCode() { |
| 69 | Throwable cause = getCause(); |
| 70 | if (cause instanceof XPathException) { |
| 71 | StructuredQName code = ((XPathException) cause).getErrorCodeQName(); |
| 72 | return code == null ? null : new QName(code); |
| 73 | } else { |
| 74 | return null; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the line number associated with the exception, if known. |
| 80 | * @return the line number (typically of a line in a stylesheet, query, or schema, but |
| 81 | * in the case of validation errors it may be a line in a source document); or -1 if not known |
| 82 | * @since 9.6 |
| 83 | */ |
| 84 | |
| 85 | public int getLineNumber() { |
| 86 | Throwable cause = getCause(); |
| 87 | if (cause instanceof XPathException) { |
| 88 | Location loc = ((XPathException) cause).getLocator(); |
| 89 | return loc == null ? -1 : loc.getLineNumber(); |
| 90 | } else { |
| 91 | return -1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the URI of the module associated with the exception, if known. |
| 97 | * @return the URI of the module (typically a stylesheet, query, or schema, but |
| 98 | * in the case of validation errors it may be a source document); or null if not known |
| 99 | * @since 9.6 |
| 100 | */ |
| 101 | |
| 102 | public String getSystemId() { |
| 103 | Throwable cause = getCause(); |
| 104 | if (cause instanceof XPathException) { |
| 105 | Location loc = ((XPathException) cause).getLocator(); |
| 106 | return loc == null ? null : loc.getSystemId(); |
| 107 | } else { |
| 108 | return null; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |