001package com.gigya.android.sdk.network; 002 003import android.support.annotation.NonNull; 004 005import com.gigya.android.sdk.api.GigyaApiResponse; 006import com.google.gson.Gson; 007 008import org.json.JSONObject; 009 010import java.util.Map; 011 012/** 013 * Main Gigya error representation class. 014 */ 015public class GigyaError extends GigyaResponseModel { 016 017 public static class Codes { 018 public static final int ERROR_ACCOUNT_PENDING_REGISTRATION = 206001; 019 public static final int ERROR_ACCOUNT_PENDING_VERIFICATION = 206002; 020 public static final int ERROR_PENDING_PASSWORD_CHANGE = 403100; 021 public static final int ERROR_LOGIN_IDENTIFIER_EXISTS = 403043; 022 public static final int ERROR_PENDING_TWO_FACTOR_REGISTRATION = 403102; 023 public static final int ERROR_PENDING_TWO_FACTOR_VERIFICATION = 403101; 024 public static final int ERROR_NETWORK = 500026; 025 026 public static final int SUCCESS_ERROR_ACCOUNT_LINKED = 200009; 027 028 public static final int ERROR_INVALID_JWT = 400006; 029 030 public static final int ERROR_REQUEST_HAS_EXPIRED = 403002; 031 } 032 033 /* Raw Json data. */ 034 private String data; 035 036 private String localizedMessage; 037 038 public GigyaError(int errorCode, String localizedMessage, String callId) { 039 this.errorCode = errorCode; 040 this.localizedMessage = localizedMessage; 041 this.callId = callId; 042 } 043 044 public GigyaError(String rawJson, int errorCode, String localizedMessage, String callId) { 045 this.data = rawJson; 046 this.errorCode = errorCode; 047 this.localizedMessage = localizedMessage; 048 this.callId = callId; 049 } 050 051 public static GigyaError generalError() { 052 return new GigyaError(400, "General error", ""); 053 } 054 055 public static GigyaError errorFrom(String message) { 056 return new GigyaError(400, message, ""); 057 } 058 059 public static GigyaError unauthorizedUser() { 060 return new GigyaError(403005, "Unauthorized user", ""); 061 } 062 063 public static GigyaError cancelledOperation() { 064 return new GigyaError(200001, "Operation canceled", ""); 065 } 066 067 public static GigyaError fromResponse(GigyaApiResponse response) { 068 final int errorCode = response.getErrorCode(); 069 final String localizedMessage = response.getErrorDetails(); 070 final String callId = response.getCallId(); 071 return new GigyaError(response.asJson(), errorCode, localizedMessage, callId); 072 } 073 074 public static GigyaError errorFrom(Map<String, Object> errorEvent) { 075 final JSONObject jsonObj = new JSONObject(errorEvent); 076 final Object errorCode = errorEvent.get("errorCode"); 077 int code = 400; 078 if (errorCode != null) { 079 if (errorCode instanceof String) { 080 code = Integer.parseInt((String) errorCode); 081 } else if (errorCode instanceof Integer) { 082 code = (int) errorCode; 083 } 084 } 085 final String errorMessage = (String) errorEvent.get("errorMessage"); 086 087 return new GigyaError( 088 jsonObj.toString(), 089 code, 090 errorMessage, 091 null 092 ); 093 } 094 095 @NonNull 096 @Override 097 public String toString() { 098 return "<< Gigya error: code: " + 099 this.errorCode + 100 ", message: " + 101 this.localizedMessage + 102 ", callId: " + 103 this.callId; 104 } 105 106 public int getErrorCode() { 107 return errorCode; 108 } 109 110 public String getLocalizedMessage() { 111 return localizedMessage; 112 } 113 114 public String getCallId() { 115 return callId; 116 } 117 118 public String getData() { 119 if (data != null) { 120 return data; 121 } 122 return new Gson().toJson(this); 123 } 124}