001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.core;
017:
018: import java.util.logging.Logger;
019: import java.io.CharArrayWriter;
020: import java.io.PrintWriter;
021:
022: /**
023: * @author yonik
024: * @version $Id: SolrException.java 542737 2007-05-30 03:13:05Z ryan $
025: */
026: public class SolrException extends RuntimeException {
027:
028: /**
029: * @since solr 1.2
030: */
031: public enum ErrorCode {
032: BAD_REQUEST(400), UNAUTHORIZED(401), FORBIDDEN(403), NOT_FOUND(
033: 404), SERVER_ERROR(500), SERVICE_UNAVAILABLE(503);
034:
035: final int code;
036:
037: private ErrorCode(int c) {
038: code = c;
039: }
040: };
041:
042: public boolean logged = false;
043:
044: public SolrException(ErrorCode code, String msg) {
045: super (msg);
046: this .code = code.code;
047: }
048:
049: public SolrException(ErrorCode code, String msg,
050: boolean alreadyLogged) {
051: super (msg);
052: this .code = code.code;
053: this .logged = alreadyLogged;
054: }
055:
056: public SolrException(ErrorCode code, String msg, Throwable th,
057: boolean alreadyLogged) {
058: super (msg, th);
059: this .code = code.code;
060: logged = alreadyLogged;
061: }
062:
063: public SolrException(ErrorCode code, String msg, Throwable th) {
064: this (code, msg, th, true);
065: }
066:
067: public SolrException(ErrorCode code, Throwable th) {
068: super (th);
069: this .code = code.code;
070: logged = true;
071: }
072:
073: @Deprecated
074: public SolrException(int code, String msg) {
075: super (msg);
076: this .code = code;
077: }
078:
079: @Deprecated
080: public SolrException(int code, String msg, boolean alreadyLogged) {
081: super (msg);
082: this .code = code;
083: this .logged = alreadyLogged;
084: }
085:
086: @Deprecated
087: public SolrException(int code, String msg, Throwable th,
088: boolean alreadyLogged) {
089: super (msg, th);
090: this .code = code;
091: logged = alreadyLogged;
092: }
093:
094: @Deprecated
095: public SolrException(int code, String msg, Throwable th) {
096: this (code, msg, th, true);
097: }
098:
099: @Deprecated
100: public SolrException(int code, Throwable th) {
101: super (th);
102: this .code = code;
103: logged = true;
104: }
105:
106: int code = 0;
107:
108: public int code() {
109: return code;
110: }
111:
112: public void log(Logger log) {
113: log(log, this );
114: }
115:
116: public static void log(Logger log, Throwable e) {
117: log.severe(toStr(e));
118: if (e instanceof SolrException) {
119: ((SolrException) e).logged = true;
120: }
121: }
122:
123: public static void log(Logger log, String msg, Throwable e) {
124: log.severe(msg + ':' + toStr(e));
125: if (e instanceof SolrException) {
126: ((SolrException) e).logged = true;
127: }
128: }
129:
130: public static void logOnce(Logger log, String msg, Throwable e) {
131: if (e instanceof SolrException) {
132: if (((SolrException) e).logged)
133: return;
134: }
135: if (msg != null)
136: log(log, msg, e);
137: else
138: log(log, e);
139: }
140:
141: // public String toString() { return toStr(this); } // oops, inf loop
142: @Override
143: public String toString() {
144: return super .toString();
145: }
146:
147: public static String toStr(Throwable e) {
148: CharArrayWriter cw = new CharArrayWriter();
149: PrintWriter pw = new PrintWriter(cw);
150: e.printStackTrace(pw);
151: pw.flush();
152: return cw.toString();
153:
154: /** This doesn't work for some reason!!!!!
155: StringWriter sw = new StringWriter();
156: PrintWriter pw = new PrintWriter(sw);
157: e.printStackTrace(pw);
158: pw.flush();
159: System.out.println("The STRING:" + sw.toString());
160: return sw.toString();
161: **/
162: }
163:
164: }
|