Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7307 -- Split InvalidPage exception into two subclasses, PageN…

…otAnInteger and EmptyPage, for granular exception catching. Thanks for the idea, miracle2k

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7867 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a724fff300671ffa78f08158d7e11281cd0a4fd1 1 parent cb70c99
Adrian Holovaty authored July 08, 2008
16  django/core/paginator.py
... ...
@@ -1,6 +1,12 @@
1 1
 class InvalidPage(Exception):
2 2
     pass
3 3
 
  4
+class PageNotAnInteger(InvalidPage):
  5
+    pass
  6
+
  7
+class EmptyPage(InvalidPage):
  8
+    pass
  9
+
4 10
 class Paginator(object):
5 11
     def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True):
6 12
         self.object_list = object_list
@@ -14,14 +20,14 @@ def validate_number(self, number):
14 20
         try:
15 21
             number = int(number)
16 22
         except ValueError:
17  
-            raise InvalidPage('That page number is not an integer')
  23
+            raise PageNotAnInteger('That page number is not an integer')
18 24
         if number < 1:
19  
-            raise InvalidPage('That page number is less than 1')
  25
+            raise EmptyPage('That page number is less than 1')
20 26
         if number > self.num_pages:
21 27
             if number == 1 and self.allow_empty_first_page:
22 28
                 pass
23 29
             else:
24  
-                raise InvalidPage('That page contains no results')
  30
+                raise EmptyPage('That page contains no results')
25 31
         return number
26 32
 
27 33
     def page(self, number):
@@ -129,14 +135,14 @@ def validate_page_number(self, page_number):
129 135
         try:
130 136
             page_number = int(page_number) + 1
131 137
         except ValueError:
132  
-            raise InvalidPage
  138
+            raise PageNotAnInteger
133 139
         return self.validate_number(page_number)
134 140
 
135 141
     def get_page(self, page_number):
136 142
         try:
137 143
             page_number = int(page_number) + 1
138 144
         except ValueError:
139  
-            raise InvalidPage
  145
+            raise PageNotAnInteger
140 146
         return self.page(page_number).object_list
141 147
 
142 148
     def has_next_page(self, page_number):
15  docs/pagination.txt
@@ -82,6 +82,21 @@ Attributes
82 82
 
83 83
 ``page_range`` -- A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``.
84 84
 
  85
+``InvalidPage`` exceptions
  86
+==========================
  87
+
  88
+The ``page()`` method raises ``InvalidPage`` if the requested page is invalid
  89
+(i.e., not an integer) or contains no objects. Generally, it's enough to trap
  90
+the ``InvalidPage`` exception, but if you'd like more granularity, you can trap
  91
+either of the following exceptions:
  92
+
  93
+``PageNotAnInteger`` -- Raised when ``page()`` is given a value that isn't an integer.
  94
+
  95
+``EmptyPage`` -- Raised when ``page()`` is given a valid value but no objects exist on that page.
  96
+
  97
+Both of the exceptions are subclasses of ``InvalidPage``, so you can handle
  98
+them both with a simple ``except InvalidPage``.
  99
+
85 100
 ``Page`` objects
86 101
 ================
87 102
 

0 notes on commit a724fff

Please sign in to comment.
Something went wrong with that request. Please try again.