Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use more specific error class

Marcin Gębalad5f92df18870
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 199e1eab7d..5e6995ed4e 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -4,6 +4,7 @@ import graphene
from django.core.exceptions import ImproperlyConfigured, ValidationError
from graphene.types.mutation import MutationOptions
from graphene_django.registry import get_global_registry
+from graphql.error import GraphQLError
from graphql_jwt import ObtainJSONWebToken, Verify
from graphql_jwt.exceptions import GraphQLJWTError, PermissionDenied
@@ -131,7 +132,7 @@ class ModelMutation(BaseMutation):
# handle ID field
elif value is not None and field.type == graphene.ID:
instance = cls.get_node_or_error(
- info, value, errors, field=field.name)
+ info, value, errors=errors, field=field.name)
cleaned_input[field_name] = instance
# handle uploaded files
@@ -213,23 +214,21 @@ class ModelMutation(BaseMutation):
instance.save()
@classmethod
- def get_node_or_error(cls, info, id, errors, only_type=None, field='Error'):
+ def get_node_or_error(cls, info, id, errors, field, only_type=None):
instance = None
try:
instance = get_node(info, id, only_type)
- except Exception as e:
- cls.add_error(
- field=field, message=str(e), errors=errors)
+ except GraphQLError as e:
+ cls.add_error(field=field, message=str(e), errors=errors)
return instance
@classmethod
- def get_nodes_or_error(cls, ids, errors, graphene_type=None, field='Error'):
+ def get_nodes_or_error(cls, ids, errors, field, graphene_type=None):
instances = None
try:
instances = get_nodes(ids, graphene_type)
- except Exception as e:
- cls.add_error(
- field=field, message=str(e), errors=errors)
+ except GraphQLError as e:
+ cls.add_error(field=field, message=str(e), errors=errors)
return instances
@classmethod
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index 03d29cd91c..3ddc0994ad 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -3,6 +3,7 @@ from django.db.models import Q
from django.utils.encoding import smart_text
from django.utils.text import slugify
from graphene_django.registry import get_global_registry
+from graphql.error import GraphQLError
from graphql_relay import from_global_id
from ..product.models import AttributeChoiceValue, ProductAttribute
@@ -15,7 +16,7 @@ def get_node(info, id, only_type=None):
"""Return node or throw an error if the node does not exist."""
node = graphene.Node.get_node_from_global_id(info, id, only_type=only_type)
if not node:
- raise Exception(
+ raise GraphQLError(
"Could not resolve to a node with the global id of '%s'." % id)
return node
@@ -52,7 +53,7 @@ def get_nodes(ids, graphene_type=None):
nodes = list(graphene_type._meta.model.objects.filter(pk__in=pks))
if not nodes:
- raise Exception(
+ raise GraphQLError(
"Could not resolve to a nodes with the global id list of '%s'."
% ids)
nodes_pk_list = [str(node.pk) for node in nodes]