Search in sources :

Example 11 with Exception

use of java.lang.Exception in project KaellyBot by Kaysoro.

the class WhoisCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        String pseudo = m.group(1).trim().toLowerCase();
        StringBuilder url;
        try {
            url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "character.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            ExceptionManager.manageException(e, message, this, lg);
            return false;
        }
        if (m.group(2) != null) {
            String serverName = m.group(2).trim().toLowerCase();
            List<ServerDofus> result = new ArrayList<>();
            for (ServerDofus server : ServerDofus.getServersDofus()) if (server.getName().toLowerCase().startsWith(serverName))
                result.add(server);
            if (result.size() == 1)
                url.append("&").append(forServer).append(result.get(0).getId());
            else {
                if (!result.isEmpty())
                    tooMuchServers.throwException(message, this, lg);
                else
                    notFoundServer.throwException(message, this, lg);
                return false;
            }
        }
        try {
            Document doc = JSoupManager.getDocument(url.toString());
            Elements elems = doc.getElementsByClass("ak-bg-odd");
            elems.addAll(doc.getElementsByClass("ak-bg-even"));
            if (!elems.isEmpty()) {
                // on boucle jusqu'à temps de trouver le bon personnage (ie le plus proche du nom donnée)
                List<String> result = new ArrayList<>();
                List<String> servers = new ArrayList<>();
                for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
                    result.add(element.child(1).select("a").attr("href"));
                    servers.add(element.child(element.children().size() - 2).text());
                }
                if (result.size() == 1) {
                    Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0));
                    if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
                        Character characPage = Character.getCharacter(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                        Message.sendEmbed(message.getChannel(), characPage.getEmbedObject(lg));
                    } else
                        characterTooOld.throwException(message, this, lg);
                } else if (result.size() > 1)
                    tooMuchCharacters.throwException(message, this, lg, servers);
                else
                    notFoundCharacter.throwException(message, this, lg);
            } else
                notFoundCharacter.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, characterPageInaccessible);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : Character(data.Character) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) Exception(java.lang.Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Language(enums.Language)

Example 12 with Exception

use of java.lang.Exception in project camunda-bpm-platform by camunda.

the class TestConnector method execute.

public TestConnectorResponse execute(TestConnectorRequest req) {
    // capture request parameters
    requestParameters = req.getRequestParameters();
    TestRequestInvocation testRequestInvocation = new TestRequestInvocation(null, req, requestInterceptors);
    try {
        testRequestInvocation.proceed();
        // use response parameters
        return new TestConnectorResponse(responseParameters);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RuntimeException(java.lang.RuntimeException) RuntimeException(java.lang.RuntimeException) Exception(java.lang.Exception)

Example 13 with Exception

use of java.lang.Exception in project java-docs-samples by GoogleCloudPlatform.

the class GaeInfoServlet method fetchJsonMetadata.

String fetchJsonMetadata(String prefix) throws IOException {
    String json = "";
    try {
        Request request = new Request.Builder().url(metadata + prefix).addHeader("Metadata-Flavor", "Google").get().build();
        Response response = ok.newCall(request).execute();
        // Convert json to prety json
        json = response.body().string();
        return gson.toJson(jp.parse(json));
    } catch (Exception e) {
        log("fetchJsonMetadata - " + metadata + prefix + " : ", e);
    }
    return "{}";
}
Also used : Response(okhttp3.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) GsonBuilder(com.google.gson.GsonBuilder) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(okhttp3.Request) IOException(java.io.IOException) Exception(java.lang.Exception)

Example 14 with Exception

use of java.lang.Exception in project clojure by clojure.

the class LispReader method read.

private static Object read(PushbackReader r, boolean eofIsError, Object eofValue, Character returnOn, Object returnOnValue, boolean isRecursive, Object opts, Object pendingForms, Resolver resolver) {
    if (RT.READEVAL.deref() == UNKNOWN)
        throw Util.runtimeException("Reading disallowed - *read-eval* bound to :unknown");
    opts = installPlatformFeature(opts);
    try {
        for (; ; ) {
            if (pendingForms instanceof List && !((List) pendingForms).isEmpty())
                return ((List) pendingForms).remove(0);
            int ch = read1(r);
            while (isWhitespace(ch)) ch = read1(r);
            if (ch == -1) {
                if (eofIsError)
                    throw Util.runtimeException("EOF while reading");
                return eofValue;
            }
            if (returnOn != null && (returnOn.charValue() == ch)) {
                return returnOnValue;
            }
            if (Character.isDigit(ch)) {
                Object n = readNumber(r, (char) ch);
                return n;
            }
            IFn macroFn = getMacro(ch);
            if (macroFn != null) {
                Object ret = macroFn.invoke(r, (char) ch, opts, pendingForms);
                // no op macros return the reader
                if (ret == r)
                    continue;
                return ret;
            }
            if (ch == '+' || ch == '-') {
                int ch2 = read1(r);
                if (Character.isDigit(ch2)) {
                    unread(r, ch2);
                    Object n = readNumber(r, (char) ch);
                    return n;
                }
                unread(r, ch2);
            }
            String token = readToken(r, (char) ch);
            return interpretToken(token, resolver);
        }
    } catch (Exception e) {
        if (isRecursive || !(r instanceof LineNumberingPushbackReader))
            throw Util.sneakyThrow(e);
        LineNumberingPushbackReader rdr = (LineNumberingPushbackReader) r;
        // throw Util.runtimeException(String.format("ReaderError:(%d,1) %s", rdr.getLineNumber(), e.getMessage()), e);
        throw new ReaderException(rdr.getLineNumber(), rdr.getColumnNumber(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Object(java.lang.Object) String(java.lang.String) IllegalStateException(java.lang.IllegalStateException) UnsupportedOperationException(java.lang.UnsupportedOperationException) IOException(java.io.IOException) NumberFormatException(java.lang.NumberFormatException) RuntimeException(java.lang.RuntimeException) IllegalArgumentException(java.lang.IllegalArgumentException) Exception(java.lang.Exception)

Example 15 with Exception

use of java.lang.Exception in project clochure by videlalvaro.

the class LispReader method read.

public static Object read(PushbackReader r, boolean eofIsError, Object eofValue, boolean isRecursive) {
    if (RT.READEVAL.deref() == UNKNOWN)
        throw Util.runtimeException("Reading disallowed - *read-eval* bound to :unknown");
    try {
        for (; ; ) {
            int ch = read1(r);
            while (isWhitespace(ch)) ch = read1(r);
            if (ch == -1) {
                if (eofIsError)
                    throw Util.runtimeException("EOF while reading");
                return eofValue;
            }
            if (Character.isDigit(ch)) {
                Object n = readNumber(r, (char) ch);
                if (RT.suppressRead())
                    return null;
                return n;
            }
            IFn macroFn = getMacro(ch);
            if (macroFn != null) {
                Object ret = macroFn.invoke(r, (char) ch);
                if (RT.suppressRead())
                    return null;
                // no op macros return the reader
                if (ret == r)
                    continue;
                return ret;
            }
            if (ch == '+' || ch == '-') {
                int ch2 = read1(r);
                if (Character.isDigit(ch2)) {
                    unread(r, ch2);
                    Object n = readNumber(r, (char) ch);
                    if (RT.suppressRead())
                        return null;
                    return n;
                }
                unread(r, ch2);
            }
            String token = readToken(r, (char) ch);
            if (RT.suppressRead())
                return null;
            return interpretToken(token);
        }
    } catch (Exception e) {
        if (isRecursive || !(r instanceof LineNumberingPushbackReader))
            throw Util.sneakyThrow(e);
        LineNumberingPushbackReader rdr = (LineNumberingPushbackReader) r;
        // throw Util.runtimeException(String.format("ReaderError:(%d,1) %s", rdr.getLineNumber(), e.getMessage()), e);
        throw new ReaderException(rdr.getLineNumber(), rdr.getColumnNumber(), e);
    }
}
Also used : Object(java.lang.Object) String(java.lang.String) IllegalStateException(java.lang.IllegalStateException) UnsupportedOperationException(java.lang.UnsupportedOperationException) IOException(java.io.IOException) NumberFormatException(java.lang.NumberFormatException) RuntimeException(java.lang.RuntimeException) IllegalArgumentException(java.lang.IllegalArgumentException) Exception(java.lang.Exception)

Aggregations

Exception (java.lang.Exception)32 IOException (java.io.IOException)15 ParseException (java.text.ParseException)12 Test (org.junit.Test)12 String (java.lang.String)11 SQLException (java.sql.SQLException)11 SQLTimeoutException (java.sql.SQLTimeoutException)11 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)11 ExpectedException (org.junit.rules.ExpectedException)11 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)7 RuntimeException (java.lang.RuntimeException)6 Statement (java.sql.Statement)6 ArrayList (java.util.ArrayList)5 IllegalArgumentException (java.lang.IllegalArgumentException)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 IllegalStateException (java.lang.IllegalStateException)3 NumberFormatException (java.lang.NumberFormatException)3 Object (java.lang.Object)3