add error_code to Exceptions

This commit is contained in:
Pierre-antoine Comby 2020-02-23 13:46:25 +01:00
parent 5af0058858
commit 201c5f667c
1 changed files with 13 additions and 8 deletions

View File

@ -64,7 +64,8 @@ class Note(PolymorphicModel):
if aliases.exists(): if aliases.exists():
# Alias exists, so check if it is linked to this note # Alias exists, so check if it is linked to this note
if aliases.first().note != self: if aliases.first().note != self:
raise ValidationError(_('This alias is already taken.')) raise ValidationError(_('This alias is already taken.'),
code="same_alias")
# Save note # Save note
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -87,7 +88,8 @@ class Note(PolymorphicModel):
if aliases.exists(): if aliases.exists():
# Alias exists, so check if it is linked to this note # Alias exists, so check if it is linked to this note
if aliases.first().note != self: if aliases.first().note != self:
raise ValidationError(_('This alias is already taken.')) raise ValidationError(_('This alias is already taken.'),
code="same_alias",)
else: else:
# Alias does not exist yet, so check if it can exist # Alias does not exist yet, so check if it can exist
a = Alias(name=str(self)) a = Alias(name=str(self))
@ -222,16 +224,19 @@ class Alias(models.Model):
def clean(self): def clean(self):
normalized_name = Alias.normalize(self.name) normalized_name = Alias.normalize(self.name)
if len(normalized_name) >= 255: if len(normalized_name) >= 255:
raise ValidationError(_('Alias too long.')) raise ValidationError(_('Alias is too long.'),
code='alias_too_long')
try: try:
if self != Alias.objects.get(normalized_name=normalized_name): sim_alias = Alias.objects.get(normalized_name=normalized_name)
raise ValidationError( if self != sim_alias:
_('An alias with a similar name ' raise ValidationError(_('An alias with a similar name already exists:'),
'already exists.')) code="same_alias"
)
except Alias.DoesNotExist: except Alias.DoesNotExist:
pass pass
def delete(self, using=None, keep_parents=False): def delete(self, using=None, keep_parents=False):
if self.name == str(self.note): if self.name == str(self.note):
raise ValidationError(_("You can't delete your main alias.")) raise ValidationError(_("You can't delete your main alias."),
code="cant_delete_main_alias")
return super().delete(using, keep_parents) return super().delete(using, keep_parents)