Skip to content

Commit 2fbab85

Browse files
author
Max Presman
committed
clean up pubnub exception
1 parent 4864d27 commit 2fbab85

12 files changed

Lines changed: 70 additions & 168 deletions

File tree

‎src/main/java/com/pubnub/api/core/Crypto.java‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void InitCiphers() throws PubnubException {
5050
ivBytes = initializationVector.getBytes("UTF-8");
5151
INIT = true;
5252
} catch (UnsupportedEncodingException e) {
53-
throw new PubnubException(newCryptoError(11, e.toString()));
53+
throw PubnubException.builder().pubnubError(newCryptoError(11, e.toString())).errormsg(e.getMessage()).build();
5454
}
5555
}
5656

@@ -61,7 +61,7 @@ public static byte[] hexEncode(byte[] input) throws PubnubException {
6161
try {
6262
return result.toString().getBytes("UTF-8");
6363
} catch (UnsupportedEncodingException e) {
64-
throw new PubnubException(newCryptoError(12, e.toString()));
64+
throw PubnubException.builder().pubnubError(newCryptoError(12, e.toString())).errormsg(e.getMessage()).build();
6565
}
6666
}
6767

@@ -79,19 +79,19 @@ public String encrypt(String input) throws PubnubException {
7979
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
8080
return new String(Base64.encode(cipher.doFinal(input.getBytes("UTF-8")), 0));
8181
} catch (NoSuchAlgorithmException e) {
82-
throw new PubnubException(e.toString());
82+
throw PubnubException.builder().errormsg(e.toString()).build();
8383
} catch (NoSuchPaddingException e) {
84-
throw new PubnubException(e.toString());
84+
throw PubnubException.builder().errormsg(e.toString()).build();
8585
} catch (InvalidKeyException e) {
86-
throw new PubnubException(e.toString());
86+
throw PubnubException.builder().errormsg(e.toString()).build();
8787
} catch (InvalidAlgorithmParameterException e) {
88-
throw new PubnubException(e.toString());
88+
throw PubnubException.builder().errormsg(e.toString()).build();
8989
} catch (UnsupportedEncodingException e) {
90-
throw new PubnubException(e.toString());
90+
throw PubnubException.builder().errormsg(e.toString()).build();
9191
} catch (IllegalBlockSizeException e) {
92-
throw new PubnubException(e.toString());
92+
throw PubnubException.builder().errormsg(e.toString()).build();
9393
} catch (BadPaddingException e) {
94-
throw new PubnubException(e.toString());
94+
throw PubnubException.builder().errormsg(e.toString()).build();
9595
}
9696

9797
}
@@ -112,21 +112,21 @@ public String decrypt(String cipher_text) throws PubnubException {
112112
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
113113
return new String(cipher.doFinal(Base64.decode(cipher_text, 0)), "UTF-8");
114114
} catch (IllegalArgumentException e) {
115-
throw new PubnubException(e.toString());
115+
throw PubnubException.builder().errormsg(e.toString()).build();
116116
} catch (UnsupportedEncodingException e) {
117-
throw new PubnubException(e.toString());
117+
throw PubnubException.builder().errormsg(e.toString()).build();
118118
} catch (IllegalBlockSizeException e) {
119-
throw new PubnubException(e.toString());
119+
throw PubnubException.builder().errormsg(e.toString()).build();
120120
} catch (BadPaddingException e) {
121-
throw new PubnubException(e.toString());
121+
throw PubnubException.builder().errormsg(e.toString()).build();
122122
} catch (InvalidKeyException e) {
123-
throw new PubnubException(e.toString());
123+
throw PubnubException.builder().errormsg(e.toString()).build();
124124
} catch (InvalidAlgorithmParameterException e) {
125-
throw new PubnubException(e.toString());
125+
throw PubnubException.builder().errormsg(e.toString()).build();
126126
} catch (NoSuchAlgorithmException e) {
127-
throw new PubnubException(e.toString());
127+
throw PubnubException.builder().errormsg(e.toString()).build();
128128
} catch (NoSuchPaddingException e) {
129-
throw new PubnubException(e.toString());
129+
throw PubnubException.builder().errormsg(e.toString()).build();
130130
}
131131
}
132132

@@ -153,9 +153,9 @@ public static byte[] md5(String input) throws PubnubException {
153153
byte[] hashedBytes = digest.digest(input.getBytes("UTF-8"));
154154
return hashedBytes;
155155
} catch (NoSuchAlgorithmException e) {
156-
throw new PubnubException(newCryptoError(118, e.toString()));
156+
throw PubnubException.builder().pubnubError(newCryptoError(118, e.toString())).errormsg(e.getMessage()).build();
157157
} catch (UnsupportedEncodingException e) {
158-
throw new PubnubException(newCryptoError(119, e.toString()));
158+
throw PubnubException.builder().pubnubError(newCryptoError(119, e.toString())).errormsg(e.getMessage()).build();
159159
}
160160
}
161161

@@ -173,7 +173,7 @@ public static byte[] sha256(byte[] input) throws PubnubException {
173173
byte[] hashedBytes = digest.digest(input);
174174
return hashedBytes;
175175
} catch (NoSuchAlgorithmException e) {
176-
throw new PubnubException(newCryptoError(1111, e.toString()));
176+
throw PubnubException.builder().pubnubError(newCryptoError(111, e.toString())).errormsg(e.getMessage()).build();
177177
}
178178
}
179179

‎src/main/java/com/pubnub/api/core/Pubnub.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public final Publish publish() {
121121
*/
122122
public final String decrypt(String inputString) throws PubnubException {
123123
if (inputString == null) {
124-
throw new PubnubException(PubnubError.PNERROBJ_INVALID_ARGUMENTS);
124+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS).build();
125125
}
126126

127127
return decrypt(inputString, this.getConfiguration().getCipherKey());
@@ -136,7 +136,7 @@ public final String decrypt(String inputString) throws PubnubException {
136136
*/
137137
public final String decrypt(final String inputString, final String cipherKey) throws PubnubException {
138138
if (inputString == null) {
139-
throw new PubnubException(PubnubError.PNERROBJ_INVALID_ARGUMENTS);
139+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS).build();
140140
}
141141

142142
return new Crypto(cipherKey).decrypt(inputString);
@@ -149,7 +149,7 @@ public final String decrypt(final String inputString, final String cipherKey) th
149149
*/
150150
public final String encrypt(String inputString) throws PubnubException {
151151
if (inputString == null) {
152-
throw new PubnubException(PubnubError.PNERROBJ_INVALID_ARGUMENTS);
152+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS).build();
153153
}
154154

155155
return encrypt(inputString, this.getConfiguration().getCipherKey());
@@ -164,7 +164,7 @@ public final String encrypt(String inputString) throws PubnubException {
164164
*/
165165
public final String encrypt(final String inputString, final String cipherKey) throws PubnubException {
166166
if (inputString == null) {
167-
throw new PubnubException(PubnubError.PNERROBJ_INVALID_ARGUMENTS);
167+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS).build();
168168
}
169169

170170
return new Crypto(cipherKey).encrypt(inputString);
Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,13 @@
11
package com.pubnub.api.core;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import lombok.Builder;
45

5-
/**
6-
* PubnubException is thrown by various Pubnub APIs
7-
*
8-
* @author PubnubCore
9-
*/
6+
@Builder
107
public class PubnubException extends Exception {
118
private String errormsg = "";
129
private PubnubError pubnubError = PubnubError.PNERROBJ_PUBNUB_ERROR;
1310
private JsonNode jso;
1411
private String response;
1512
private int statusCode;
16-
17-
public int getStatusCode() {
18-
return statusCode;
19-
}
20-
21-
public void setStatusCode(int statusCode) {
22-
this.statusCode = statusCode;
23-
}
24-
25-
/**
26-
* Constructor for PubnubException Class with error message as argument
27-
*
28-
* @param s
29-
* Error message
30-
*/
31-
public PubnubException(String s) {
32-
this.errormsg = s;
33-
}
34-
35-
/**
36-
* Constructor for PubnubException Class with error message as argument
37-
*
38-
* @param pubnubError
39-
* Error message
40-
*/
41-
public PubnubException(PubnubError pubnubError) {
42-
this.pubnubError = pubnubError;
43-
}
44-
45-
/**
46-
* Constructor for PubnubException Class with error message as argument
47-
*
48-
* @param s
49-
* Error message
50-
*/
51-
public PubnubException(PubnubError pubnubError, String s) {
52-
this.errormsg = s;
53-
this.pubnubError = pubnubError;
54-
}
55-
56-
/**
57-
* Constructor for PubnubException Class with error message as argument
58-
*
59-
* @param s
60-
* Error message
61-
*/
62-
public PubnubException(PubnubError pubnubError, String s, int statusCode) {
63-
this.errormsg = s;
64-
this.pubnubError = pubnubError;
65-
this.statusCode = statusCode;
66-
}
67-
68-
/**
69-
* Constructor for PubnubException Class with error message as argument
70-
*
71-
* @param pubnubError
72-
* @param s
73-
* Error message
74-
* @param response
75-
* @param jso
76-
*/
77-
public PubnubException(PubnubError pubnubError, String s, String response, JsonNode jso) {
78-
this.errormsg = s;
79-
this.pubnubError = pubnubError;
80-
this.jso = jso;
81-
this.response = response;
82-
}
83-
84-
/**
85-
* Constructor for PubnubException Class with error message as argument
86-
*
87-
* @param pubnubError
88-
* @param response
89-
* @param jso
90-
*/
91-
public PubnubException(PubnubError pubnubError, String response, JsonNode jso) {
92-
this.pubnubError = pubnubError;
93-
this.jso = jso;
94-
this.response = response;
95-
}
96-
97-
/**
98-
* Constructor for PubnubException Class with error message as argument
99-
*
100-
* @param pubnubError
101-
* @param response
102-
* @param jso
103-
* @param statusCode
104-
*/
105-
public PubnubException(PubnubError pubnubError, String response, JsonNode jso, int statusCode) {
106-
this.pubnubError = pubnubError;
107-
this.jso = jso;
108-
this.response = response;
109-
this.statusCode = statusCode;
110-
}
111-
112-
/**
113-
* Read the exception error message
114-
*
115-
* @return String
116-
*/
117-
public String toString() {
118-
String msg = pubnubError.toString();
119-
if (errormsg.length() > 0)
120-
msg = msg + " . " + errormsg;
121-
return msg;
122-
}
123-
124-
public PubnubError getPubnubError() {
125-
return pubnubError;
126-
}
127-
128-
public String getErrorResponse() {
129-
return response;
130-
}
131-
132-
public JsonNode getErrorJsonObject() {
133-
return jso;
134-
}
135-
13613
}

‎src/main/java/com/pubnub/api/endpoints/Endpoint.java‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final Output sync() throws PubnubException {
4545
try {
4646
serverResponse = call.execute();
4747
} catch (IOException e) {
48-
throw new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR);
48+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_HTTP_ERROR).build();
4949
}
5050

5151
if (!serverResponse.isSuccessful() || serverResponse.code() != 200) {
@@ -57,7 +57,11 @@ public final Output sync() throws PubnubException {
5757
responseBodyText = "N/A";
5858
}
5959

60-
throw new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, responseBodyText, serverResponse.code());
60+
throw PubnubException.builder()
61+
.pubnubError(PubnubError.PNERROBJ_HTTP_ERROR)
62+
.errormsg(responseBodyText)
63+
.statusCode(serverResponse.code())
64+
.build();
6165
}
6266

6367
response = createResponse(serverResponse);
@@ -72,7 +76,12 @@ public final Call<Input> async(final PNCallback<Output> callback) {
7276
try {
7377
call = doWork(createBaseParams());
7478
} catch (PubnubException e) {
75-
PubnubException pubnubException = new PubnubException(PubnubError.PNERROBJ_INVALID_ARGUMENTS, e.getMessage());
79+
80+
PubnubException pubnubException = PubnubException.builder()
81+
.pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS)
82+
.errormsg(e.getMessage())
83+
.build();
84+
7685
PNErrorStatus pnErrorStatus = new PNErrorStatus();
7786
writeFieldsToResponse(null, pnErrorStatus, true, pubnubException);
7887
callback.onResponse(null, pnErrorStatus);
@@ -96,7 +105,12 @@ public void onResponse(final Call<Input> call, final Response<Input> response) {
96105
responseBodyText = "N/A";
97106
}
98107

99-
PubnubException ex = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, responseBodyText, response.code());
108+
PubnubException ex = PubnubException.builder()
109+
.pubnubError(PubnubError.PNERROBJ_HTTP_ERROR)
110+
.errormsg(responseBodyText)
111+
.statusCode(response.code())
112+
.build();
113+
100114
writeFieldsToResponse(response, pnErrorStatus, true, ex);
101115
callback.onResponse(null, pnErrorStatus);
102116
return;
@@ -105,7 +119,13 @@ public void onResponse(final Call<Input> call, final Response<Input> response) {
105119
try {
106120
callbackResponse = createResponse(response);
107121
} catch (PubnubException e) {
108-
PubnubException pubnubException = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, e.getMessage(), response.code());
122+
123+
PubnubException pubnubException = PubnubException.builder()
124+
.pubnubError(PubnubError.PNERROBJ_HTTP_ERROR)
125+
.errormsg(e.getMessage())
126+
.statusCode(response.code())
127+
.build();
128+
109129
writeFieldsToResponse(response, pnErrorStatus, true, pubnubException);
110130
callback.onResponse(null, pnErrorStatus);
111131
return;
@@ -118,7 +138,12 @@ public void onResponse(final Call<Input> call, final Response<Input> response) {
118138
@Override
119139
public void onFailure(final Call<Input> call, final Throwable throwable) {
120140
PNErrorStatus pnErrorStatus = new PNErrorStatus();
121-
PubnubException pubnubException = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, throwable.getMessage(), null);
141+
142+
PubnubException pubnubException = PubnubException.builder()
143+
.pubnubError(PubnubError.PNERROBJ_HTTP_ERROR)
144+
.errormsg(throwable.getMessage())
145+
.build();
146+
122147
writeFieldsToResponse(null, pnErrorStatus, true, pubnubException);
123148
callback.onResponse(null, pnErrorStatus);
124149

@@ -152,19 +177,19 @@ protected String signSHA256(String key, String data) throws PubnubException {
152177
try {
153178
sha256_HMAC = Mac.getInstance("HmacSHA256");
154179
} catch (NoSuchAlgorithmException e) {
155-
throw new PubnubException(PubnubError.PNERROBJ_CRYPTO_ERROR, e.getMessage(), null);
180+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
156181
}
157182

158183
try {
159184
sha256_HMAC.init(secretKey);
160185
} catch (InvalidKeyException e) {
161-
throw new PubnubException(PubnubError.PNERROBJ_CRYPTO_ERROR, e.getMessage(), null);
186+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
162187
}
163188

164189
try {
165190
hmacData = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
166191
} catch (UnsupportedEncodingException e) {
167-
throw new PubnubException(PubnubError.PNERROBJ_CRYPTO_ERROR, e.getMessage(), null);
192+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
168193
}
169194

170195
return new String(Base64.encode(hmacData, 0)).replace('+', '-').replace('/', '_');

‎src/main/java/com/pubnub/api/endpoints/History.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private Object processMessage(JsonNode message) throws PubnubException {
126126
try {
127127
outputObject = mapper.readValue(outputText, JsonNode.class);
128128
} catch (IOException e) {
129-
throw new PubnubException(PubnubError.PNERROBJ_PARSING_ERROR);
129+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_PARSING_ERROR).errormsg(e.getMessage()).build();
130130
}
131131

132132
return outputObject;

‎src/main/java/com/pubnub/api/endpoints/Time.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected final PNTimeResult createResponse(final Response<List<Long>> input) th
4040
PNTimeResult timeData = new PNTimeResult();
4141

4242
if (input.body() == null || input.body().size() == 0) {
43-
throw new PubnubException(PubnubError.PNERROBJ_PARSING_ERROR);
43+
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_PARSING_ERROR).build();
4444
}
4545

4646
timeData.setTimetoken(input.body().get(0));

0 commit comments

Comments
 (0)