All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eg.agent.android.util.Error Maven / Gradle / Ivy

The newest version!
package com.eg.agent.android.util;

import com.eg.agent.android.logging.EGAgentLog;
import com.eg.agent.android.logging.EGAgentLogManager;
import com.eg.agent.android.networkinformation.ExceptionUtils;
import com.eg.agent.android.EGAgent;

import org.json.JSONObject;

import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Venkateswari.J on 4/25/2017.
 */


public class Error {
    private final String url;
    private final int httpStatusCode;
    private int count;
    private int errorCode;
    private final String responseBody;
    private final String stackTrace;
    private final Map params;
    private final String digest;
    private static final EGAgentLog log = EGAgentLogManager.getAgentLog();
    
    public Error(int errorCode) {
    	this(null, 0, null, null);    
    	this.errorCode = errorCode;
    	
    }

    public Error(String url, int httpStatusCode, String responseBody, Map params) {
        this.url = url;
        this.httpStatusCode = httpStatusCode;
        this.count = 1;
        this.responseBody = responseBody;
        this.stackTrace = this.getSanitizedStackTrace();
        this.params = params;
        this.digest = this.computeHash();
    }

    public String getUrl() {
        return this.url;
    }

    public int getHttpStatusCode() {
        return this.httpStatusCode;
    }

    public int getCount() {
        return this.count;
    }

    public String getResponseBody() {
        return this.responseBody;
    }

    public String getStackTrace() {
        return this.stackTrace;
    }

    public Map getParams() {
        return this.params;
    }

    public String getHash() {
        return this.digest;
    }

    public List asList() {
        ArrayList list = new ArrayList();
        list.add(this.url);
        list.add(Integer.valueOf(this.httpStatusCode));
        list.add(Integer.valueOf(this.count));
        list.add(this.responseBody);
        list.add(this.stackTrace);
        TreeMap custom_params = new TreeMap();
        custom_params.put("custom_params", new JSONObject(this.params));
        list.add(new JSONObject(custom_params));
        return list;
    }

    public void incrementCount() {
        ++this.count;
    }

    public String computeHash() {
        MessageDigest digester;
        try {
            digester = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException var3) {
            log.error("Unable to initialize SHA-1 hash algorithm");
            return null;
        }

        digester.update(this.url.getBytes());
        digester.update(ByteBuffer.allocate(8).putInt(this.httpStatusCode).array());
        if(this.stackTrace != null && this.stackTrace.length() > 0) {
            digester.update(this.stackTrace.getBytes());
        }

        return new String(digester.digest());
    }

    public String getSanitizedStackTrace() {
        StringBuilder stackTrace = new StringBuilder();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        int numErrors = 0;

        for(int i = 0; i < stackTraceElements.length; ++i) {
            StackTraceElement frame = stackTraceElements[i];
            if(!this.shouldFilterStackTraceElement(frame)) {
                stackTrace.append(frame.toString());
                if(i <= stackTraceElements.length - 1) {
                    stackTrace.append("\n");
                }

                ++numErrors;
                if(numErrors >= EGAgent.getStackTraceLimit()) {
                    break;
                }
            }
        }

        return stackTrace.toString();
    }
    
    public static int exceptionToErrorCode(Exception e) {
    	   return ExceptionUtils.exceptionToErrorCode(e);
    }
    
    public static Error exceptionToError(Exception e) {
 	   return new Error(ExceptionUtils.exceptionToErrorCode(e));
 }
    
    public int getErrorCode() {
    	    return this.errorCode;
     }

    private boolean shouldFilterStackTraceElement(StackTraceElement element) {
        String className = element.getClassName();
        String method = element.getMethodName();
        return className.startsWith("com.eg")?true:(className.startsWith("dalvik.system.VMStack") && method.startsWith("getThreadStackTrace")?true:className.startsWith("java.lang.Thread") && method.startsWith("getStackTrace"));
    }
}