Added support for error category to ApiCommandException which was weirdly missing. Not complete

This commit is contained in:
Gideon Le Grange 2020-01-03 09:03:22 +02:00
parent 1f31a6f3b5
commit 8fa8711ec5
3 changed files with 34 additions and 13 deletions

View File

@ -4,31 +4,40 @@ import me.legrange.mikrotik.MikrotikApiException;
/**
* Thrown when the Mikrotik returns an error when receiving our command.
*
* @author GideonLeGrange
*/
public class ApiCommandException extends MikrotikApiException {
private String tag = null;
private int category = 0;
/** return the tag associated with this exception, if there is one
* @return the tag associated with this exception. Null if there is no tag*/
/**
* return the tag associated with this exception, if there is one
*
* @return the tag associated with this exception. Null if there is no tag
*/
public String getTag() {
return tag;
}
ApiCommandException(String msg) {
ApiCommandException(String msg) {
super(msg);
}
ApiCommandException(String msg, Throwable err) {
ApiCommandException(String msg, Throwable err) {
super(msg, err);
}
public int getCategory() {
return category;
}
ApiCommandException(Error err) {
super(err.getMessage());
tag = err.getTag();
category = err.getCategory();
}
private String tag = null;
}

View File

@ -248,7 +248,7 @@ public final class ApiConnectionImpl extends ApiConnection {
} catch (ApiCommandException ex) {
String tag = ex.getTag();
if (tag != null) {
res = new Error(tag, ex.getMessage());
res = new Error(tag, ex.getMessage(), ex.getCategory());
} else {
continue;
}
@ -415,6 +415,9 @@ public final class ApiConnectionImpl extends ApiConnection {
} else if (line.startsWith("=message=")) {
err.setMessage(line.split("=", 3)[2]);
}
else if (line.startsWith("=category=")) {
err.setCategory(Integer.parseInt(line.split("=", 3)[2]));
}
if (hasNextLine()) {
nextLine();
} else {

View File

@ -1,12 +1,16 @@
package me.legrange.mikrotik.impl;
/**
* Used to encapsulate API error information. We need to pass both the message and the tag (if one was used).
* Used to encapsulate API error information. We need to pass both the message and the tag (if one was used).
*
* @author GideonLeGrange
*/
class Error extends Response {
Error(String tag, String message) {
private String message;
private int category;
Error(String tag, String message, int category) {
super(tag);
this.message = message;
}
@ -18,11 +22,16 @@ class Error extends Response {
String getMessage() {
return message;
}
void setMessage(String message) {
this.message = message;
}
private String message;
int getCategory() {
return category;
}
void setCategory(int category) {
this.category = category;
}
}