Opened 5 years ago

Closed 5 years ago

#30587 closed New feature (duplicate)

Cloaking exceptions raised by get_object_or_404().

Reported by: Ishwar Bhat Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

When we use get_object_or_404() method to query an object, sometimes due to invalid inputs it will through exceptions. Instead can it raise 404 directly?
Ex:

Models:

>>class Test1(models.Model):
>>    c2 = models.CharField(max_length=10)

>>class Test2(models.Model):
>>    c1 = models.UUIDField(primary_key=True, default=uuid.uuid4)
>>    c2 = models.CharField(max_length=10)

query:

  1. Below statement raises valueError:
        >> get_object_or_404(Test1, pk='test') 
        >> - invalid literal for int() with base 10: 'test'
    
  2. Below statement raises ValidationError:
        >> get_object_or_404(Test2, pk="test")
        >> - 'test' is not a valid UUID.
    

So this method will throw different exceptions in different scenario. So would it be possible to raise 404 exceptions even if it finds invalid values.

Change History (2)

comment:1 by Adwait Thattey, 5 years ago

Yes this behavior exists.

But it seems more appropriate to throw the errors instead of 404.
This seems to be more of a server issue of trying to compare strings with integers rather than an inappropriate client input

comment:2 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Description: modified (diff)
Resolution: duplicate
Status: newclosed
Summary: Exceptions thrown by 'get_object_or_404()' shortcut methodCloaking exceptions raised by get_object_or_404().
Version: 2.2master

Duplicate of #22303.

Note: See TracTickets for help on using tickets.
Back to Top