Skip to content

Commit

Permalink
~ javatots rewrite catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Prud'hommeaux committed Dec 7, 2021
1 parent de1cc05 commit 58ab9c5
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 102 deletions.
28 changes: 16 additions & 12 deletions asTypescript/packages/client-http/src/HttpResourceAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ export class HttpResourceAccessor implements ResourceAccessor {
if (location.isPresent()) {
try {
url = new URL(location.get());
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Retrieving <" + url + "> yielded a Location header \"" + location.get() + "\" which doesn't parse as a URL: " + e.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Retrieving <" + url + "> yielded a Location header \"" + location.get() + "\" which doesn't parse as a URL: " + e.getMessage());
}
}
// Determine whether the resource exists based on the response. Even if the resource
// doesn't exist, additional context and processing is done to provide the appropriate
Expand Down Expand Up @@ -350,9 +351,10 @@ export class HttpResourceAccessor implements ResourceAccessor {
containedInstances.add(containedInstance);
}
return containedInstances;
} catch (ex: Exception) {
throw new ShapeTreeException(500, ex.getMessage());
}
} catch (ex) {
if (ex instanceof Exception) {
throw new ShapeTreeException(500, ex.getMessage());
}
}

/**
Expand Down Expand Up @@ -451,9 +453,10 @@ export class HttpResourceAccessor implements ResourceAccessor {
let managerUrlString: string = optManagerString.get();
try {
return Optional.of(new URL(url, managerUrlString));
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Malformed relative URL <" + managerUrlString + "> (resolved from <" + url + ">)");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed relative URL <" + managerUrlString + "> (resolved from <" + url + ">)");
}
}

/**
Expand All @@ -479,9 +482,10 @@ export class HttpResourceAccessor implements ResourceAccessor {
}
try {
managedResourceUrl = new URL(managerUrl, managedUrlString);
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Can't calculate managed resource for shape tree manager <" + managerUrl + ">");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Can't calculate managed resource for shape tree manager <" + managerUrl + ">");
}
return managedResourceUrl;
}

Expand Down
7 changes: 4 additions & 3 deletions asTypescript/packages/core/src/ManageableResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export class ManageableResource extends InstanceResource {
const rel: string = this.isContainer() ? ".." : ".";
try {
return new URL(this.getUrl(), rel);
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + rel + "> against <" + this.getUrl() + ">");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + rel + "> against <" + this.getUrl() + ">");
}
}

public getManagerResourceUrl(): URL | null {
Expand Down
2 changes: 1 addition & 1 deletion asTypescript/packages/core/src/ResourceAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ResourceAttributes {
* @param attr attribute (header) name to set
* @param value String value to assign to attr
*/
public constructor(attr: string, value: string) throws ShapeTreeException {
public constructor(attr: string, value: string) /* throws ShapeTreeException */ {
this.myMapOfLists = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.maybeSet(attr, value);
}
Expand Down
14 changes: 8 additions & 6 deletions asTypescript/packages/core/src/ShapeTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ export class ShapeTree {
if (SchemaCache.isInitialized()) {
SchemaCache.putSchema(this.shape, schema);
}
} catch (ex: Exception) {
throw new ShapeTreeException(500, "Error parsing ShEx schema - " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof Exception) {
throw new ShapeTreeException(500, "Error parsing ShEx schema - " + ex.getMessage());
}
}
// Tell ShExJava we want to use Jena as our graph library
let jenaRDF: JenaRDF = new org.apache.commons.rdf.jena.JenaRDF();
Expand Down Expand Up @@ -147,9 +148,10 @@ export class ShapeTree {
const matchingFocusNode: URL;
try {
matchingFocusNode = new URL(focusUriString);
} catch (ex: MalformedURLException) {
throw new ShapeTreeException(500, "Error reporting validation success on malformed URL <" + focusUriString + ">: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Error reporting validation success on malformed URL <" + focusUriString + ">: " + ex.getMessage());
}
return new ValidationResult(valid, this, this, matchingFocusNode);
}
}
Expand Down
9 changes: 5 additions & 4 deletions asTypescript/packages/core/src/ShapeTreeAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ShapeTreeAssignment {

private readonly url: URL;

public constructor(shapeTree: URL, managedResource: URL, rootAssignment: URL, focusNode: URL, shape: URL, url: URL) throws ShapeTreeException {
public constructor(shapeTree: URL, managedResource: URL, rootAssignment: URL, focusNode: URL, shape: URL, url: URL) /* throws ShapeTreeException */ {
try {
this.shapeTree = Objects.requireNonNull(shapeTree, "Must provide an assigned shape tree");
this.managedResource = Objects.requireNonNull(managedResource, "Must provide a shape tree context");
Expand All @@ -53,9 +53,10 @@ export class ShapeTreeAssignment {
}
this.focusNode = null;
}
} catch (ex: NullPointerException | IllegalStateException) {
throw new ShapeTreeException(500, "Failed to initialize shape tree assignment: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof NullPointerException || ex instanceof IllegalStateException) {
throw new ShapeTreeException(500, "Failed to initialize shape tree assignment: " + ex.getMessage());
}
}

public static getFromGraph(url: URL, managerGraph: Graph): ShapeTreeAssignment /* throws MalformedURLException, ShapeTreeException */ {
Expand Down
21 changes: 12 additions & 9 deletions asTypescript/packages/core/src/ShapeTreeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ export class ShapeTreeFactory {
private static getContains(resourceModel: Model, shapeTreeNode: Resource, shapeTreeUrl: URL): Array<URL> /* throws ShapeTreeException */ {
try {
return getURLListValue(resourceModel, shapeTreeNode, ShapeTreeVocabulary.CONTAINS);
} catch (ex: MalformedURLException | ShapeTreeException) {
throw new ShapeTreeException(500, "List <" + shapeTreeUrl + "> contains malformed URL: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException || ex instanceof ShapeTreeException) {
throw new ShapeTreeException(500, "List <" + shapeTreeUrl + "> contains malformed URL: " + ex.getMessage());
}
}

/**
Expand All @@ -113,9 +114,10 @@ export class ShapeTreeFactory {
let referencedShapeTree: ShapeTreeReference;
try {
referencedShapeTreeUrl = new URL(referencedShapeTreeUrlString);
} catch (ex: MalformedURLException) {
throw new ShapeTreeException(500, "ShapeTree <" + shapeTreeUrl + "> references malformed URL <" + referencedShapeTreeUrlString + ">: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "ShapeTree <" + shapeTreeUrl + "> references malformed URL <" + referencedShapeTreeUrlString + ">: " + ex.getMessage());
}
let viaShapePath: string = getStringValue(resourceModel, referenceResource, ShapeTreeVocabulary.VIA_SHAPE_PATH);
let viaPredicate: URL = getUrlValue(resourceModel, referenceResource, ShapeTreeVocabulary.VIA_PREDICATE, shapeTreeUrl);
referencedShapeTree = new ShapeTreeReference(referencedShapeTreeUrl, viaShapePath, viaPredicate);
Expand All @@ -142,9 +144,10 @@ export class ShapeTreeFactory {
if (object.isURIResource()) {
try {
return new URL(object.asResource().getURI());
} catch (ex: MalformedURLException) {
throw new IllegalStateException("Malformed ShapeTree <" + shapeTreeUrl + ">: Jena URIResource <" + object + "> didn't parse as URL - " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Malformed ShapeTree <" + shapeTreeUrl + ">: Jena URIResource <" + object + "> didn't parse as URL - " + ex.getMessage());
}
} else {
throw new ShapeTreeException(500, "Malformed ShapeTree <" + shapeTreeUrl + ">: expected " + object + " to be a URL");
}
Expand Down
14 changes: 8 additions & 6 deletions asTypescript/packages/core/src/ShapeTreeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export class ShapeTreeManager {
const assignmentUrl: URL;
try {
assignmentUrl = new URL(assignmentString);
} catch (ex: MalformedURLException) {
throw new IllegalStateException("Minted illegal URL <" + assignmentString + "> - " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Minted illegal URL <" + assignmentString + "> - " + ex.getMessage());
}
return assignmentUrl;
}

Expand Down Expand Up @@ -163,9 +164,10 @@ export class ShapeTreeManager {
let assignment: ShapeTreeAssignment = null;
try {
assignment = ShapeTreeAssignment.getFromGraph(new URL(assignmentNode.getObject().getURI()), managerGraph);
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Object of { " + s + " " + stAssignment + " " + assignmentNode.getObject() + " } must be a URL.");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Object of { " + s + " " + stAssignment + " " + assignmentNode.getObject() + " } must be a URL.");
}
manager.assignments.add(assignment);
}
return manager;
Expand Down
7 changes: 4 additions & 3 deletions asTypescript/packages/core/src/ShapeTreeManagerDelta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ export class ShapeTreeManagerDelta {
try {
assignmentUri = assignment.getUrl().toURI();
targetAssignmentUri = targetAssignment.getUrl().toURI();
} catch (ex: URISyntaxException) {
throw new ShapeTreeException(500, "Unable to convert assignment URLs for comparison: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof URISyntaxException) {
throw new ShapeTreeException(500, "Unable to convert assignment URLs for comparison: " + ex.getMessage());
}
if (assignmentUri === targetAssignmentUri) {
return targetAssignment;
}
Expand Down
2 changes: 1 addition & 1 deletion asTypescript/packages/core/src/ShapeTreeReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ShapeTreeReference {

readonly predicate: URL;

public constructor(referenceUrl: URL, shapePath: string, predicate: URL) throws ShapeTreeException {
public constructor(referenceUrl: URL, shapePath: string, predicate: URL) /* throws ShapeTreeException */ {
this.referenceUrl = Objects.requireNonNull(referenceUrl);
if (shapePath === null && predicate === null) {
throw new ShapeTreeException(500, "Shape tree reference must have either a shape path or a predicate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export class HttpExternalDocumentLoader implements ExternalDocumentLoader {
}
let attributes: ResourceAttributes = new ResourceAttributes(response.headers().map());
return new DocumentResponse(attributes, response.body(), response.statusCode());
} catch (ex: IOException) {
throw new ShapeTreeException(500, "Error retrieving <" + resourceUrl + ">: " + ex.getMessage());
} catch (ex: InterruptedException) {
Thread.currentThread().interrupt();
throw new ShapeTreeException(500, "Error retrieving <" + resourceUrl + ">: " + ex.getMessage());
} catch (ex: URISyntaxException) {
throw new ShapeTreeException(500, "Malformed URL <" + resourceUrl + ">: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof IOException) {
throw new ShapeTreeException(500, "Error retrieving <" + resourceUrl + ">: " + ex.getMessage());
} else if (ex instanceof InterruptedException) {
Thread.currentThread().interrupt();
throw new ShapeTreeException(500, "Error retrieving <" + resourceUrl + ">: " + ex.getMessage());
} else if (ex instanceof URISyntaxException) {
throw new ShapeTreeException(500, "Malformed URL <" + resourceUrl + ">: " + ex.getMessage());
}
}
}
28 changes: 16 additions & 12 deletions asTypescript/packages/core/src/helpers/GraphHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ export class GraphHelper {
let reader: StringReader = new StringReader(rawContent);
RDFDataMgr.read(model.getGraph(), reader, baseURI.toString(), GraphHelper.getLangForContentType(contentType));
return model;
} catch (rex: RiotException) {
throw new ShapeTreeException(422, "Error processing input - " + rex.getMessage());
}
} catch (ex) {
if (ex instanceof RiotException) {
throw new ShapeTreeException(422, "Error processing input - " + rex.getMessage());
}
}

/**
Expand Down Expand Up @@ -160,9 +161,10 @@ export class GraphHelper {
public static urlToUri(url: URL): URI {
try {
return url.toURI();
} catch (ex: URISyntaxException) {
throw new IllegalStateException("can't convert URL <" + url + "> to IRI: " + ex);
}
} catch (ex) {
if (ex instanceof URISyntaxException) {
throw new IllegalStateException("can't convert URL <" + url + "> to IRI: " + ex);
}
}

/**
Expand All @@ -178,16 +180,18 @@ export class GraphHelper {
try {
let noFragment: URI = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), null);
return noFragment.toURL();
} catch (ex: MalformedURLException | URISyntaxException) {
throw new IllegalStateException("Unable to remove fragment from URL: " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException || ex instanceof URISyntaxException) {
throw new IllegalStateException("Unable to remove fragment from URL: " + ex.getMessage());
}
}

public static knownUrl(urlString: string): URL {
try {
return new URL(urlString);
} catch (ex: MalformedURLException) {
throw new IllegalStateException("Expected known URL <" + urlString + "> to parse as valid URL - " + ex.toString());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Expected known URL <" + urlString + "> to parse as valid URL - " + ex.toString());
}
}
}
21 changes: 12 additions & 9 deletions asTypescript/packages/core/src/helpers/RequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ export class RequestHelper {
try {
const focusNodeUrl: URL = new URL(baseUrl, focusNodeUrlString);
focusNodeUrls.add(focusNodeUrl);
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + focusNodeUrlString + "> against <" + baseUrl + ">");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + focusNodeUrlString + "> against <" + baseUrl + ">");
}
}
}
return focusNodeUrls;
Expand All @@ -119,9 +120,10 @@ export class RequestHelper {
try {
const targetShapeTreeUrl: URL = new URL(targetShapeTreeUrlString);
targetShapeTreeUrls.add(targetShapeTreeUrl);
} catch (e: MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + targetShapeTreeUrlString + "> against <" + baseUrl + ">");
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + targetShapeTreeUrlString + "> against <" + baseUrl + ">");
}
}
}
return targetShapeTreeUrls;
Expand Down Expand Up @@ -153,9 +155,10 @@ export class RequestHelper {
}
try {
return new URL(urlString);
} catch (ex: MalformedURLException) {
throw new ShapeTreeException(500, "normalized to malformed URL <" + urlString + "> - " + ex.getMessage());
}
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "normalized to malformed URL <" + urlString + "> - " + ex.getMessage());
}
}

/**
Expand Down
Loading

0 comments on commit 58ab9c5

Please sign in to comment.