Reformat with 2-space indents
[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,
20     String host, int port) {
21     int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
22     String message;
23     switch (errorCode) {
24       case yaz4jlibConstants.ZOOM_ERROR_NONE:
25         return null;
26       case yaz4jlib.ZOOM_ERROR_CONNECT:
27         message = String.format("Connection could not be made to %s:%d", host,
28           port);
29         return new ConnectionUnavailableException(message);
30       case yaz4jlib.ZOOM_ERROR_INVALID_QUERY:
31         message = String.format(
32           "The query requested is not valid or not supported");
33         return new InvalidQueryException(message);
34       case yaz4jlib.ZOOM_ERROR_INIT:
35         message = String.format("Server %s:%d rejected our init request", host,
36           port);
37         return new InitRejectedException(message);
38       case yaz4jlib.ZOOM_ERROR_TIMEOUT:
39         message = String.format("Server %s:%d timed out handling our request",
40           host, port);
41         return new ConnectionTimeoutException(message);
42       case yaz4jlib.ZOOM_ERROR_MEMORY:
43       case yaz4jlib.ZOOM_ERROR_ENCODE:
44       case yaz4jlib.ZOOM_ERROR_DECODE:
45       case yaz4jlib.ZOOM_ERROR_CONNECTION_LOST:
46       case yaz4jlib.ZOOM_ERROR_INTERNAL:
47       case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
48       case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY:
49         message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
50         return new ZoomImplementationException("A fatal error occurred in Yaz: "
51           + errorCode + " - " + message);
52       default:
53         String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.
54           getError(errorCode) + ")";
55         return new Bib1Exception(errMsgBib1);
56     }
57   }
58 }