Skip to content

Commit 0d476be

Browse files
Allow2 Devruvnet
andcommitted
Add exception classes
Allow2Exception base with context map, ApiException with HTTP status, TokenExpiredException and UnpairedException with userId tracking. Co-Authored-By: claude-flow <ruv@ruv.net>
1 parent 4c0554e commit 0d476be

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.allow2.service.exceptions;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Base exception for all Allow2 SDK errors.
9+
*/
10+
public class Allow2Exception extends RuntimeException {
11+
12+
private final Map<String, Object> context;
13+
14+
public Allow2Exception(String message, int code, Throwable cause, Map<String, Object> context) {
15+
super(message, cause);
16+
this.context = context != null ? Collections.unmodifiableMap(new HashMap<>(context)) : Collections.emptyMap();
17+
}
18+
19+
public Allow2Exception(String message) {
20+
this(message, 0, null, null);
21+
}
22+
23+
public Allow2Exception(String message, Throwable cause) {
24+
this(message, 0, cause, null);
25+
}
26+
27+
/**
28+
* Get the context map with additional error details.
29+
*
30+
* @return An unmodifiable context map.
31+
*/
32+
public Map<String, Object> getContext() {
33+
return context;
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.allow2.service.exceptions;
2+
3+
import org.json.JSONObject;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* Thrown when an Allow2 API call fails with an unexpected error.
10+
*/
11+
public class ApiException extends Allow2Exception {
12+
13+
private final int httpStatusCode;
14+
private final JSONObject responseBody;
15+
16+
public ApiException(String message, int httpStatusCode, JSONObject responseBody) {
17+
this(message, httpStatusCode, responseBody, null);
18+
}
19+
20+
public ApiException(String message, int httpStatusCode, JSONObject responseBody, Throwable cause) {
21+
super(message, 0, cause, buildContext(httpStatusCode, responseBody));
22+
this.httpStatusCode = httpStatusCode;
23+
this.responseBody = responseBody;
24+
}
25+
26+
public int getHttpStatusCode() {
27+
return httpStatusCode;
28+
}
29+
30+
public JSONObject getResponseBody() {
31+
return responseBody;
32+
}
33+
34+
private static Map<String, Object> buildContext(int httpStatusCode, JSONObject responseBody) {
35+
Map<String, Object> ctx = new HashMap<>();
36+
ctx.put("httpStatusCode", httpStatusCode);
37+
if (responseBody != null) {
38+
ctx.put("responseBody", responseBody.toString());
39+
}
40+
return ctx;
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.allow2.service.exceptions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Thrown when the OAuth2 token refresh fails and no valid token is available.
8+
*/
9+
public class TokenExpiredException extends Allow2Exception {
10+
11+
private final String userId;
12+
13+
public TokenExpiredException(String userId, String message) {
14+
this(userId, message, null);
15+
}
16+
17+
public TokenExpiredException(String userId, String message, Throwable cause) {
18+
super(message, 0, cause, buildContext(userId));
19+
this.userId = userId;
20+
}
21+
22+
public TokenExpiredException(String userId) {
23+
this(userId, "OAuth2 token refresh failed. Re-authorization required.", null);
24+
}
25+
26+
public String getUserId() {
27+
return userId;
28+
}
29+
30+
private static Map<String, Object> buildContext(String userId) {
31+
Map<String, Object> ctx = new HashMap<>();
32+
ctx.put("userId", userId);
33+
return ctx;
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.allow2.service.exceptions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Thrown when the service account is no longer linked (HTTP 401/403 from API).
8+
*
9+
* <p>The integration should redirect the user through OAuth2 re-pairing.</p>
10+
*/
11+
public class UnpairedException extends Allow2Exception {
12+
13+
private final String userId;
14+
15+
public UnpairedException(String userId, String message) {
16+
this(userId, message, null);
17+
}
18+
19+
public UnpairedException(String userId, String message, Throwable cause) {
20+
super(message, 0, cause, buildContext(userId));
21+
this.userId = userId;
22+
}
23+
24+
public UnpairedException(String userId) {
25+
this(userId, "Service account is no longer linked. Re-pairing required.", null);
26+
}
27+
28+
public String getUserId() {
29+
return userId;
30+
}
31+
32+
private static Map<String, Object> buildContext(String userId) {
33+
Map<String, Object> ctx = new HashMap<>();
34+
ctx.put("userId", userId);
35+
return ctx;
36+
}
37+
}

0 commit comments

Comments
 (0)