Use built-in graphql errors

This commit is contained in:
Christian Cueni 2020-05-12 09:56:48 +02:00
parent edd431671f
commit 685fa3ff34
8 changed files with 24 additions and 55 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,5 @@
import { GraphQLError } from "graphql";
const schema = require('../fixtures/schema.json'); const schema = require('../fixtures/schema.json');
describe('Email Verifcation', () => { describe('Email Verifcation', () => {
@ -10,13 +12,10 @@ describe('Email Verifcation', () => {
cy.mockGraphql({ cy.mockGraphql({
schema: schema, schema: schema,
operations: { operations: {
Coupon: variables => { Coupon: {
return {
coupon: { coupon: {
errors: [],
success: true success: true
} }
}
}, },
} }
}); });
@ -45,16 +44,7 @@ describe('Email Verifcation', () => {
cy.mockGraphql({ cy.mockGraphql({
schema: schema, schema: schema,
operations: { operations: {
Coupon: variables => { Coupon: new GraphQLError("invalid_coupon")
return {
coupon: {
errors: [{
field: 'invalid_coupon'
}],
success: false
}
}
},
} }
}); });
cy.login('rahel.cueni', 'test', true) cy.login('rahel.cueni', 'test', true)
@ -71,16 +61,7 @@ describe('Email Verifcation', () => {
cy.mockGraphql({ cy.mockGraphql({
schema: schema, schema: schema,
operations: { operations: {
Coupon: variables => { Coupon: new GraphQLError("unknown_error")
return {
coupon: {
errors: [{
field: 'unknown_error'
}],
success: false
}
}
},
} }
}); });
cy.login('rahel.cueni', 'test', true) cy.login('rahel.cueni', 'test', true)

View File

@ -1,8 +1,5 @@
mutation Coupon($input: CouponInput!){ mutation Coupon($input: CouponInput!){
coupon(input: $input) { coupon(input: $input) {
success success
errors {
field
}
} }
} }

View File

@ -64,7 +64,8 @@ export default {
this.errorMessage = 'Ein Fehler ist aufgetreten. Bitte kontaktieren Sie den Administrator.'; this.errorMessage = 'Ein Fehler ist aufgetreten. Bitte kontaktieren Sie den Administrator.';
} }
} }
}); })
.catch(() => this.errorMessage = 'Ein Fehler ist aufgetreten. Bitte kontaktieren Sie den Administrator.');
}, },
}; };

View File

@ -80,14 +80,14 @@ export default {
query: ME_QUERY, query: ME_QUERY,
fetchPolicy: 'network-only', fetchPolicy: 'network-only',
}).then(() => that.$router.push('/')); }).then(() => that.$router.push('/'));
} else { }
if (coupon.errors[0].field === 'invalid_coupon') { }
}).catch(({message}) => {
if (message.indexOf('invalid_coupon') > -1) {
that.couponErrors = ['Der angegebene Coupon-Code ist falsch.']; that.couponErrors = ['Der angegebene Coupon-Code ist falsch.'];
} else { } else {
that.couponErrors = ['Es ist ein Fehler aufgetreten. Bitte versuchen Sie es nochmals oder kontaktieren Sie den Administrator.']; that.couponErrors = ['Es ist ein Fehler aufgetreten. Bitte versuchen Sie es nochmals oder kontaktieren Sie den Administrator.'];
} }
}
}
}); });
} }
}); });

View File

@ -14,16 +14,11 @@ from core.hep_client import HepClient, HepClientException
from users.user_signup_login_handler import check_and_create_licenses, create_role_for_user from users.user_signup_login_handler import check_and_create_licenses, create_role_for_user
class CouponError(graphene.ObjectType):
field = graphene.String()
class Coupon(relay.ClientIDMutation): class Coupon(relay.ClientIDMutation):
class Input: class Input:
coupon_code_input = graphene.String() coupon_code_input = graphene.String()
success = graphene.Boolean() success = graphene.Boolean()
errors = graphene.List(CouponError) # todo: change for consistency
@classmethod @classmethod
def mutate_and_get_payload(cls, root, info, **kwargs): def mutate_and_get_payload(cls, root, info, **kwargs):
@ -33,25 +28,25 @@ class Coupon(relay.ClientIDMutation):
try: try:
hep_id = info.context.user.hep_id hep_id = info.context.user.hep_id
except AttributeError: except AttributeError:
return cls(success=False, errors=[{'field': 'not_authenticated'}]) raise Exception('not_authenticated')
try: try:
response = hep_client.coupon_redeem(coupon_code, hep_id) response = hep_client.coupon_redeem(coupon_code, hep_id)
except HepClientException: except HepClientException:
return cls(success=False, errors=[{'field': 'unknown_error'}]) raise Exception('unknown_error')
if not response: if not response:
return cls(success=False, errors=[{'field': 'invalid_coupon'}]) raise Exception('invalid_coupon')
license, error_msg = check_and_create_licenses(hep_client, info.context.user) license, error_msg = check_and_create_licenses(hep_client, info.context.user)
# todo fail if no license # todo fail if no license
if error_msg: if error_msg:
return cls(success=False, errors=[error_msg]) raise Exception(error_msg)
create_role_for_user(info.context.user, license.for_role.key) create_role_for_user(info.context.user, license.for_role.key)
return cls(success=True, errors=[]) return cls(success=True)
class CouponMutations: class CouponMutations:

View File

@ -42,9 +42,6 @@ class CouponTests(TestCase):
mutation Coupon($input: CouponInput!){ mutation Coupon($input: CouponInput!){
coupon(input: $input) { coupon(input: $input) {
success success
errors {
field
}
} }
} }
''' '''
@ -76,8 +73,7 @@ class CouponTests(TestCase):
def test_user_has_invalid_coupon(self, response_mock): def test_user_has_invalid_coupon(self, response_mock):
result = self.make_coupon_mutation('COUPON--1234', self.client) result = self.make_coupon_mutation('COUPON--1234', self.client)
self.assertFalse(result.get('data').get('coupon').get('success')) self.assertEqual(result.get('errors')[0].get('message'), 'invalid_coupon')
self.assertEqual(result.get('data').get('coupon').get('errors')[0].get('field'), 'invalid_coupon')
@patch.object(requests, 'put', return_value=MockResponse(200, data=['201', 'Invalid Coupon'])) @patch.object(requests, 'put', return_value=MockResponse(200, data=['201', 'Invalid Coupon']))
def test_unauthenticated_user_cannot_redeem(self, response_mock): def test_unauthenticated_user_cannot_redeem(self, response_mock):
@ -90,5 +86,4 @@ class CouponTests(TestCase):
result = self.make_coupon_mutation('COUPON--1234', client) result = self.make_coupon_mutation('COUPON--1234', client)
self.assertFalse(result.get('data').get('coupon').get('success')) self.assertEqual(result.get('errors')[0].get('message'), 'not_authenticated')
self.assertEqual(result.get('data').get('coupon').get('errors')[0].get('field'), 'not_authenticated')