Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because a
4
 *             problem occurred in the metacat database
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2010-12-21 14:26:06 -0800 (Tue, 21 Dec 2010) $'
11
 * '$Revision: 5752 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.io.IOException;
31
import java.io.PrintWriter;
32
import java.io.Writer;
33

    
34
/**
35
 * Exception thrown when an error occurs because a problem occurred in
36
 * the metacat database.  This is the general type of Exception that is
37
 * thrown whenever the server encounters an Error or Exception that is
38
 * outside of the scope of normal operation.  This class may be 
39
 * subclassed to provide more detailed informatin about the error.
40
 */
41
public class McdbException extends Exception {
42

    
43
  /**
44
   * @serial The embedded exception if wrapping an Exception
45
   */
46
  private Exception exception;
47

    
48
  /**
49
   * Create a new McdbException.
50
   */
51
  public McdbException() {
52
    super();
53
    this.exception = null;
54
  }
55

    
56
  /**
57
   * Create a new McdbException.
58
   *
59
   * @param message The error or warning message.
60
   */
61
  public McdbException(String message) {
62
    super(message);
63
    this.exception = null;
64
  }
65

    
66
  /**
67
   * Create a new McdbException.
68
   *
69
   * @param e The exception to tunnel inside this exception
70
   */
71
  public McdbException(Exception e) {
72
    super();
73
    this.exception = e;
74
  }
75

    
76
  /**
77
   * Create a new McdbException.
78
   *
79
   * @param message The error or warning message.
80
   * @param e The exception to tunnel inside this exception
81
   */
82
  public McdbException(String message, Exception e) {
83
    super(message);
84
    this.exception = e;
85
  }
86

    
87
  /**
88
   * Get the tunneled Exception
89
   */
90
  public Exception getException() {
91
    return exception;
92
  }
93

    
94
  /**
95
   * Get the message from this exception.
96
   *
97
   * <p>This returns the message from this exception, but if it
98
   * is null, and if the tunnelled exception is not null, then
99
   * it returns the message fromthe tunnelled exception.
100
   */
101
  public String getMessage() {
102
    String msg = super.getMessage();
103

    
104
    if (msg == null && exception != null) {
105
      return exception.getMessage();
106
    }
107

    
108
    return msg;
109
  }
110

    
111
  /**
112
   * Print the message from this exception in XML format.
113
   *
114
   * <p>This returns the message from this exception, but if it
115
   * is null, and if the tunnelled exception is not null, then
116
   * it returns the message from the tunnelled exception.
117
   */
118
  public void toXml(Writer pw) {
119
    String msg = super.getMessage();
120

    
121
    if (msg == null && exception != null) {
122
      msg = exception.getMessage();
123
    }
124

    
125
    try {
126
		pw.write("<?xml version=\"1.0\"?>");
127
		pw.write("<error>" + msg + "</error>");
128
	    pw.flush();
129
	} catch (IOException e) {
130
		e.printStackTrace();
131
	}
132
    
133
  }
134
}
(41-41/64)