Skip to content

Commit

Permalink
[36] Avoid using costly Exception for package not found errors
Browse files Browse the repository at this point in the history
Bug: #36
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid authored and sbegaudeau committed Jun 5, 2024
1 parent e13d502 commit d735322
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,22 +12,26 @@
*******************************************************************************/
package org.eclipse.sirius.emfjson.resource;

import java.util.Objects;

import org.eclipse.emf.ecore.resource.Resource;

/**
* This exception is throw when a package is not found.
*
* @author <a href="mailto:[email protected]">Guillaume Coutable</a>
*/
public class PackageNotFoundException extends JsonException {
public class PackageNotFoundError implements Resource.Diagnostic {

/**
* .
* The uri that correspond to the missing package.
*/
private static final long serialVersionUID = 1L;
protected final String uri;

/**
* The uri that correspond to the missing package.
* The source location of the issue.
*/
protected final String uri;
private final String location;

/**
* The constructor.
Expand All @@ -37,9 +41,29 @@ public class PackageNotFoundException extends JsonException {
* @param location
* the location
*/
public PackageNotFoundException(String uri, String location) {
super("Package with uri '" + uri + "' not found.", location); //$NON-NLS-1$//$NON-NLS-2$
this.uri = uri;
public PackageNotFoundError(String uri, String location) {
this.uri = Objects.requireNonNull(uri);
this.location = Objects.requireNonNull(location);
}

@Override
public String getMessage() {
return String.format("Package with uri '%s' not found.", this.uri); //$NON-NLS-1$
}

@Override
public String getLocation() {
return this.location;
}

@Override
public int getColumn() {
return 0;
}

@Override
public int getLine() {
return 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import org.eclipse.sirius.emfjson.resource.JsonResource.IEObjectHandler;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.URIHandler;
import org.eclipse.sirius.emfjson.resource.PackageNotFoundException;
import org.eclipse.sirius.emfjson.resource.PackageNotFoundError;

/**
* The Gson deserializer is responsible for the deserialization of EObjects.
Expand Down Expand Up @@ -795,7 +795,7 @@ private EPackage getPackageForURI(String uriString) {
}

if (ePackage == null) {
this.helper.getResource().getErrors().add(new PackageNotFoundException(uriString, this.helper.getResourceURI().toString()));
this.helper.getResource().getErrors().add(new PackageNotFoundError(uriString, this.helper.getResourceURI().toString()));
}

return ePackage;
Expand Down

0 comments on commit d735322

Please sign in to comment.