Rewrite exception handling
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / ExceptionUtil.java
1 /*
2  * Copyright (c) 1995-2010, Index Data
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package org.yaz4j;
7
8 import org.yaz4j.exception.*;
9 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
10 import org.yaz4j.jni.yaz4jlib;
11 import org.yaz4j.jni.yaz4jlibConstants;
12
13 /**
14  * Utility class to map ZOOM errors into ZoomExceptions
15  * @author jakub
16  */
17 class ExceptionUtil {
18
19   static ZoomException getError(SWIGTYPE_p_ZOOM_connection_p zoomConnection, String host, int port) {
20     int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
21     String message;
22     switch (errorCode) {
23       case yaz4jlibConstants.ZOOM_ERROR_NONE:
24         return null;
25       case yaz4jlib.ZOOM_ERROR_CONNECT:
26         message = String.format("Connection could not be made to %s:%d", host,
27         port);
28         return new ConnectionUnavailableException(message);
29       case yaz4jlib.ZOOM_ERROR_INVALID_QUERY:
30         message = String.format(
31         "The query requested is not valid or not supported");
32         return new InvalidQueryException(message);
33       case yaz4jlib.ZOOM_ERROR_INIT:
34         message = String.format("Server %s:%d rejected our init request", host,
35         port);
36         return new InitRejectedException(message);
37       case yaz4jlib.ZOOM_ERROR_TIMEOUT:
38         message = String.format("Server %s:%d timed out handling our request",
39         host, port);
40         return new ConnectionTimeoutException(message);
41       case yaz4jlib.ZOOM_ERROR_MEMORY:
42       case yaz4jlib.ZOOM_ERROR_ENCODE:
43       case yaz4jlib.ZOOM_ERROR_DECODE:
44       case yaz4jlib.ZOOM_ERROR_CONNECTION_LOST:
45       case yaz4jlib.ZOOM_ERROR_INTERNAL:
46       case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
47       case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY:
48         message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
49         return new ZoomImplementationException("A fatal error occurred in Yaz: " +
50           errorCode + " - " + message);
51       default:
52         String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" +
53           Bib1Diagnostic.getError(errorCode) + ")";
54         return new Bib1Exception(errMsgBib1);
55     }
56   }
57
58 }