Throw NotFoundException on MissingSchemaSource 00/106300/7
authorŠimon Ukuš <simon.ukus@pantheon.tech>
Wed, 31 May 2023 08:48:03 +0000 (10:48 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 6 Jun 2023 08:32:16 +0000 (08:32 +0000)
Throwing NotFoundException, instead of a more general
WebApplicationException, will return 404 status code to the client,
rather than 500 with the WebAppException.

JIRA: NETCONF-964
Change-Id: Ic72b068a477a98f618bf303c43d7deca94254f3e
Signed-off-by: Šimon Ukuš <simon.ukus@pantheon.tech>
netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java

index dfd783eb04b45e1afb003319b5f6d823c3266cfc..1a100aad279b41388c0ff44299ae0661b7a80cdd 100644 (file)
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
+import javax.ws.rs.NotFoundException;
 import javax.ws.rs.WebApplicationException;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
@@ -38,6 +39,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
 import org.opendaylight.yanglib.api.YangLibService;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -195,6 +197,9 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan
             final var yangTextSchemaSource = yangTextSchemaFuture.get();
             return yangTextSchemaSource.asCharSource(StandardCharsets.UTF_8).read();
         } catch (InterruptedException | ExecutionException e) {
+            if (e.getCause() instanceof MissingSchemaSourceException) {
+                throw new NotFoundException("Schema source " + sourceId + " not found", e);
+            }
             throw new WebApplicationException("Unable to get schema " + sourceId, e);
         } catch (IOException e) {
             throw new WebApplicationException("Unable to read schema " + sourceId, e);