Skip to content

Commit 4c253de

Browse files
committed
Add error catching in Key.Activate
1 parent c418ab7 commit 4c253de

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/main/java/io/cryptolens/methods/Key.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ public class Key {
4141
* @return A LicenseKey object if success and null otherwise.
4242
*/
4343
public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel model) {
44+
return Activate(token, RSAPubKey, model,null);
45+
}
46+
47+
/**
48+
* Calls the Activate method (https://app.cryptolens.io/docs/api/v3/Activate).
49+
* <p>This method allows you to retrieve the error message from the Web API.</p>
50+
* <p>
51+
* To retrieve the error message, you need to initialize an APIError object and pass it in into the
52+
* "error" parameter. For example,
53+
* </p>
54+
* <code>
55+
* APIError error = new APIError();<br>
56+
* LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()), error);<br>
57+
* System.out.println(error.Message);
58+
* </code>
59+
* @param token The access token with 'Activate' permission.
60+
* @param RSAPubKey Your RSA Public Key, which can be found at https://app.cryptolens.io/docs/api/v3/QuickStart.
61+
* @param model Method parameters.
62+
* @param error The error object whose Message field will be populated if an error has occurred. Please initialize
63+
* this parameter, i.e. define <code>APIError error = new APIError();</code> and then pass
64+
* <code>error</code> into this parameter.
65+
* @return A LicenseKey object if success and null otherwise.
66+
*/
67+
public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel model, APIError error) {
4468

4569
Map<String,String> extraParams = new HashMap<>();
4670

@@ -53,6 +77,9 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel
5377

5478
if(result == null || result.result == 1) {
5579
if(result != null) {
80+
if (error != null) {
81+
error.Message = result.message;
82+
}
5683
System.err.println("The server returned an error: " + result.message);
5784
} else {
5885
System.err.println("The server returned an error.");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.cryptolens.models;
2+
3+
/**
4+
* Used to store the error message obtained from the Web API.
5+
*/
6+
public class APIError {
7+
public String Message;
8+
9+
public APIError() { Message = ""; }
10+
11+
public APIError(String message) {
12+
Message = message;
13+
}
14+
}

src/test/java/io/cryptolens/KeyTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.cryptolens.methods.Helpers;
44
import io.cryptolens.methods.Key;
5+
import io.cryptolens.models.APIError;
56
import io.cryptolens.models.ActivateModel;
67
import io.cryptolens.models.DeactivateModel;
78
import io.cryptolens.models.LicenseKey;
@@ -41,10 +42,13 @@ public void testApp() throws Exception {
4142
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
4243
String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";
4344

44-
LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()));
45+
APIError error = new APIError();
46+
47+
LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()), error);
4548

4649
if (license == null || !Helpers.IsOnRightMachine(license)) {
4750
System.out.println("The license does not work.");
51+
System.out.println("Error: " + error.Message);
4852
} else {
4953
System.out.println("The license is valid!");
5054
System.out.println("It will expire: " + license.Expires);

0 commit comments

Comments
 (0)