com.wanikani.wklib.ApplicationException.java Source code

Java tutorial

Introduction

Here is the source code for com.wanikani.wklib.ApplicationException.java

Source

package com.wanikani.wklib;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

/* 
 *  Copyright (c) 2013 Alberto Cuda
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class ApplicationException extends IOException {

    private static final long serialVersionUID = 1L;

    String code;

    public ApplicationException(String code, String msg) {
        super(msg);

        this.code = code;
    }

    public static ApplicationException buildFromJSON(JSONObject obj) throws IOException {
        String code;
        String msg;

        try {
            obj = obj.getJSONObject("error");
            code = obj.getString("code");
            msg = obj.getString("message");
        } catch (JSONException e) {
            throw new ParseException();
        }

        if (code.equals(AuthenticationException.CODE_STRING))
            throw new AuthenticationException(code, msg);

        return new ApplicationException(code, msg);
    }
}