Skip to content

Commit

Permalink
refactor: Some updates to error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Nov 24, 2024
1 parent a339f81 commit 41bd250
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public ResolveRepresentationResult resolveRepresentation(DID did, Map<String, Ob

if (resolveRepresentationResult != null && resolveRepresentationResult.isErrorResult()) {
if (log.isWarnEnabled()) log.warn(resolveRepresentationResult.getError() + " -> " + resolveRepresentationResult.getErrorMessage());
throw new ResolutionException(resolveRepresentationResult);
throw ResolutionException.fromResolveResult(resolveRepresentationResult);
}

if (resolveRepresentationResult == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
if (log.isWarnEnabled()) log.warn("Driver: Resolve problem for " + didString + ": " + ex.getMessage(), ex);

if (! (ex instanceof ResolutionException)) ex = new ResolutionException("Driver: Resolve problem for " + didString + ": " + ex.getMessage());
resolveRepresentationResult = ((ResolutionException) ex).toErrorResult(accept);
resolveRepresentationResult = ((ResolutionException) ex).toErrorResolveRepresentationResult(accept);
}

if (log.isInfoEnabled()) log.info("Driver: Resolve result for " + didString + ": " + resolveRepresentationResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ResolveRepresentationResult resolveRepresentation(String didString, Map<S

if (resolveRepresentationResult != null && resolveRepresentationResult.isErrorResult()) {
if (log.isWarnEnabled()) log.warn(resolveRepresentationResult.getError() + " -> " + resolveRepresentationResult.getErrorMessage());
throw new ResolutionException(resolveRepresentationResult);
throw ResolutionException.fromResolveResult(resolveRepresentationResult);
}

if (resolveRepresentationResult == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,26 @@ public class DereferencingException extends Exception {
private final String error;
private final Map<String, Object> dereferencingMetadata;

private final DereferenceResult dereferenceResult;
private DereferenceResult dereferenceResult;

public DereferencingException(String error, String message, Map<String, Object> dereferencingMetadata, Throwable ex) {
super(message, ex);
this.error = error;
this.dereferencingMetadata = dereferencingMetadata;
this.dereferenceResult = null;
}

public DereferencingException(String error, String message, Map<String, Object> dereferencingMetadata) {
super(message);
this.error = error;
this.dereferencingMetadata = dereferencingMetadata;
this.dereferenceResult = null;
}

public DereferencingException(String error, String message, Throwable ex) {
this(error, message, null, ex);
this(error, message, (Map<String, Object>) null, ex);
}

public DereferencingException(String error, String message) {
this(error, message, null, null);
this(error, message, (Map<String, Object>) null);
}

public DereferencingException(String message, Throwable ex) {
Expand All @@ -59,31 +57,30 @@ public DereferencingException(String message) {
this(ERROR_INTERNALERROR, message);
}

public DereferencingException(DereferenceResult dereferenceResult) {
super(dereferenceResult.getErrorMessage());
if (! dereferenceResult.isErrorResult()) throw new IllegalArgumentException("No error result: " + dereferenceResult);
this.error = dereferenceResult.getError();
this.dereferencingMetadata = dereferenceResult.getDereferencingMetadata();
this.dereferenceResult = dereferenceResult;
public static DereferencingException fromDereferenceResult(DereferenceResult dereferenceResult) {
if (dereferenceResult != null && dereferenceResult.isErrorResult()) {
DereferencingException dereferencingException = new DereferencingException(dereferenceResult.getError(), dereferenceResult.getErrorMessage(), dereferenceResult.getDereferencingMetadata());
dereferencingException.dereferenceResult = dereferenceResult;
return dereferencingException;
} else {
throw new IllegalArgumentException("No error result: " + dereferenceResult);
}
}

/*
* Error methods
*/

public DereferenceResult toErrorResult(String contentType) {
if (this.getDereferenceResult() != null) {
return this.getDereferenceResult();
} else {
DereferenceResult dereferenceResult = DereferenceResult.build();
if (this.getError() != null) dereferenceResult.setError(this.getError());
if (this.getMessage() != null) dereferenceResult.setErrorMessage(this.getMessage());
if (this.getDereferencingMetadata() != null) dereferenceResult.getDereferencingMetadata().putAll(this.getDereferencingMetadata());
dereferenceResult.setContentStream(new byte[0]);
dereferenceResult.setContentType(contentType);
if (log.isDebugEnabled()) log.debug("Created error dereference result: " + dereferenceResult);
return dereferenceResult;
}
public DereferenceResult toErrorDereferenceResult(String contentType) {
if (this.dereferenceResult != null) return this.dereferenceResult;
DereferenceResult dereferenceResult = DereferenceResult.build();
if (this.getError() != null) dereferenceResult.setError(this.getError());
if (this.getMessage() != null) dereferenceResult.setErrorMessage(this.getMessage());
if (this.getDereferencingMetadata() != null) dereferenceResult.getDereferencingMetadata().putAll(this.getDereferencingMetadata());
dereferenceResult.setContentStream(new byte[0]);
dereferenceResult.setContentType(contentType);
if (log.isDebugEnabled()) log.debug("Created error dereference result: " + dereferenceResult);
return dereferenceResult;
}

/*
Expand All @@ -97,9 +94,5 @@ public String getError() {
public Map<String, Object> getDereferencingMetadata() {
return dereferencingMetadata;
}

public DereferenceResult getDereferenceResult() {
return dereferenceResult;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@

public class ResolutionException extends Exception {

/*
* From DID Core
*/

public static final String ERROR_INVALIDDID = "invalidDid";
public static final String ERROR_NOTFOUND = "notFound";
public static final String ERROR_REPRESENTATIONNOTSUPPORTED = "representationNotSupported";

/*
* From DID Resolution
*/

public static final String ERROR_METHODNOTSUPPORTED = "methodNotSupported";
public static final String ERROR_INTERNALERROR = "internalError";

Expand All @@ -30,28 +21,26 @@ public class ResolutionException extends Exception {
private final String error;
private final Map<String, Object> didResolutionMetadata;

private final ResolveResult resolveResult;
private ResolveResult resolveResult;

public ResolutionException(String error, String message, Map<String, Object> didResolutionMetadata, Throwable ex) {
super(message, ex);
this.error = error;
this.didResolutionMetadata = didResolutionMetadata;
this.resolveResult = null;
}

public ResolutionException(String error, String message, Map<String, Object> didResolutionMetadata) {
super(message);
this.error = error;
this.didResolutionMetadata = didResolutionMetadata;
this.resolveResult = null;
}

public ResolutionException(String error, String message, Throwable ex) {
this(error, message, null, ex);
this(error, message, (Map<String, Object>) null, ex);
}

public ResolutionException(String error, String message) {
this(error, message, null, null);
this(error, message, (Map<String, Object>) null);
}

public ResolutionException(String message, Throwable ex) {
Expand All @@ -66,53 +55,53 @@ public ResolutionException(Throwable ex) {
this(ex.getMessage(), ex);
}

public ResolutionException(ResolveResult resolveResult) {
super(resolveResult.getErrorMessage());
if (! resolveResult.isErrorResult()) throw new IllegalArgumentException("No error result: " + resolveResult);
this.error = resolveResult.getError();
this.didResolutionMetadata = resolveResult.getDidResolutionMetadata();
this.resolveResult = resolveResult;
public static ResolutionException fromResolveResult(ResolveResult resolveResult) {
if (resolveResult != null && resolveResult.isErrorResult()) {
ResolutionException resolutionException = new ResolutionException(resolveResult.getError(), resolveResult.getErrorMessage(), resolveResult.getDidResolutionMetadata());
resolutionException.resolveResult = resolveResult;
return resolutionException;
} else {
throw new IllegalArgumentException("No error result: " + resolveResult);
}
}

/*
* Error methods
*/

public ResolveDataModelResult toErrorResult() {
if (this.getResolveResult() != null) {
try {
return this.getResolveResult().toResolveDataModelResult();
public ResolveDataModelResult toErrorResolveDataModelResult() {
if (this.resolveResult != null) {
try {
return this.resolveResult.toResolveDataModelResult();
} catch (ResolutionException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
} else {
ResolveDataModelResult resolveDataModelResult = ResolveDataModelResult.build();
if (this.getError() != null) resolveDataModelResult.setError(this.getError());
if (this.getMessage() != null) resolveDataModelResult.setErrorMessage(this.getMessage());
if (this.getDidResolutionMetadata() != null) resolveDataModelResult.getDidResolutionMetadata().putAll(this.getDidResolutionMetadata());
resolveDataModelResult.setDidDocument(null);
if (log.isDebugEnabled()) log.debug("Created error resolve result: " + resolveDataModelResult);
return resolveDataModelResult;
}
}
}
ResolveDataModelResult resolveDataModelResult = ResolveDataModelResult.build();
if (this.getError() != null) resolveDataModelResult.setError(this.getError());
if (this.getMessage() != null) resolveDataModelResult.setErrorMessage(this.getMessage());
if (this.getDidResolutionMetadata() != null) resolveDataModelResult.getDidResolutionMetadata().putAll(this.getDidResolutionMetadata());
resolveDataModelResult.setDidDocument(null);
if (log.isDebugEnabled()) log.debug("Created error resolve result: " + resolveDataModelResult);
return resolveDataModelResult;
}

public ResolveRepresentationResult toErrorResult(String contentType) {
if (this.getResolveResult() != null) {
try {
return this.getResolveResult().toResolveRepresentationResult(contentType);
} catch (ResolutionException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
} else {
ResolveRepresentationResult resolveRepresentationResult = ResolveRepresentationResult.build();
if (this.getError() != null) resolveRepresentationResult.setError(this.getError());
if (this.getMessage() != null) resolveRepresentationResult.setErrorMessage(this.getMessage());
if (this.getDidResolutionMetadata() != null) resolveRepresentationResult.setDidResolutionMetadata(this.getDidResolutionMetadata());
resolveRepresentationResult.setDidDocumentStream(new byte[0]);
resolveRepresentationResult.setContentType(contentType);
if (log.isDebugEnabled()) log.debug("Created error resolve result: " + resolveRepresentationResult);
return resolveRepresentationResult;
}
public ResolveRepresentationResult toErrorResolveRepresentationResult(String contentType) {
if (this.resolveResult != null) {
try {
return this.resolveResult.toResolveRepresentationResult(contentType);
} catch (ResolutionException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}
ResolveRepresentationResult resolveRepresentationResult = ResolveRepresentationResult.build();
if (this.getError() != null) resolveRepresentationResult.setError(this.getError());
if (this.getMessage() != null) resolveRepresentationResult.setErrorMessage(this.getMessage());
if (this.getDidResolutionMetadata() != null) resolveRepresentationResult.getDidResolutionMetadata().putAll(this.getDidResolutionMetadata());
resolveRepresentationResult.setDidDocumentStream(new byte[0]);
resolveRepresentationResult.setContentType(contentType);
if (log.isDebugEnabled()) log.debug("Created error resolve representation result: " + resolveRepresentationResult);
return resolveRepresentationResult;
}

/*
Expand All @@ -126,8 +115,4 @@ public String getError() {
public Map<String, Object> getDidResolutionMetadata() {
return didResolutionMetadata;
}

public ResolveResult getResolveResult() {
return resolveResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
if (log.isWarnEnabled()) log.warn("Resolve problem for " + didString + ": " + ex.getMessage(), ex);

if (! (ex instanceof ResolutionException)) ex = new ResolutionException("Resolve problem for " + didString + ": " + ex.getMessage());
resolveRepresentationResult = ((ResolutionException) ex).toErrorResult(accept);
resolveRepresentationResult = ((ResolutionException) ex).toErrorResolveRepresentationResult(accept);
}

if (log.isInfoEnabled()) log.info("Resolve representation result for " + didString + ": " + resolveRepresentationResult);
Expand Down

0 comments on commit 41bd250

Please sign in to comment.