This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
errors.py
308 lines (261 loc) · 14.2 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
# Inc. All Rights Reserved.
###############################################################################
from webob.exc import HTTPBadRequest, HTTPForbidden, status_map
from r2.lib.utils import Storage, tup
from pylons import g, request
from pylons.i18n import _
from copy import copy
error_list = dict((
('USER_REQUIRED', _("please sign in to do that")),
('HTTPS_REQUIRED', _("this page must be accessed using https")),
('WRONG_DOMAIN', _("you can't do that on this domain")),
('VERIFIED_USER_REQUIRED', _("you need to set a valid email address to do that.")),
('NO_URL', _('a url is required')),
('BAD_URL', _('you should check that url')),
('INVALID_SCHEME', _('URI scheme must be one of: %(schemes)s')),
('BAD_CAPTCHA', _('care to try these again?')),
('BAD_USERNAME', _('invalid user name')),
('USERNAME_TOO_SHORT', _('username must be between %(min)d and %(max)d characters')),
('USERNAME_INVALID_CHARACTERS', _('username must contain only letters, numbers, "-", and "_"')),
('USERNAME_TAKEN', _('that username is already taken')),
('USERNAME_TAKEN_DEL', _('that username is taken by a deleted account')),
('USER_BLOCKED', _("you can't send to a user that you have blocked")),
('NO_THING_ID', _('id not specified')),
('TOO_MANY_THING_IDS', _('you provided too many ids')),
('NOT_AUTHOR', _("you can't do that")),
('NOT_USER', _("you are not signed in as that user")),
('NOT_FRIEND', _("you are not friends with that user")),
('LOGGED_IN', _("you are already signed in")),
('DELETED_LINK', _('the link you are commenting on has been deleted')),
('DELETED_COMMENT', _('that comment has been deleted')),
('DELETED_THING', _('that element has been deleted')),
('SHORT_PASSWORD', _('the password must be at least %(chars)d characters')),
('BAD_PASSWORD', _('that password is unacceptable')),
('WRONG_PASSWORD', _('wrong password')),
('BAD_PASSWORD_MATCH', _('passwords do not match')),
('NO_NAME', _('please enter a name')),
('NO_EMAIL', _('please enter an email address')),
('NO_EMAIL_FOR_USER', _('no email address for that user')),
('NO_VERIFIED_EMAIL', _('no verified email address for that user')),
('NO_TO_ADDRESS', _('send it to whom?')),
('NO_SUBJECT', _('please enter a subject')),
('USER_DOESNT_EXIST', _("that user doesn't exist")),
('NO_USER', _('please enter a username')),
('INVALID_PREF', _("that preference isn't valid")),
('NON_PREFERENCE', _("'%(choice)s' is not a user preference field")),
('INVALID_LANG', _("that language is not available")),
('BAD_NUMBER', _("that number isn't in the right range (%(range)s)")),
('BAD_STRING', _("you used a character here that we can't handle")),
('BAD_BID', _("your budget must be at least $%(min)d and no more than $%(max)d.")),
('ALREADY_SUB', _("that link has already been submitted")),
('SUBREDDIT_EXISTS', _('that subreddit already exists')),
('SUBREDDIT_NOEXIST', _('that subreddit doesn\'t exist')),
('SUBREDDIT_NOTALLOWED', _("you aren't allowed to post there.")),
('SUBREDDIT_NO_ACCESS', _("you aren't allowed access to this subreddit")),
('SUBREDDIT_REQUIRED', _('you must specify a subreddit')),
('SUBREDDIT_DISABLED_ADS', _('this subreddit has chosen to disable their ads at this time')),
('BAD_SR_NAME', _('that name isn\'t going to work')),
('COLLECTION_NOEXIST', _('that collection doesn\'t exist')),
('INVALID_TARGET', _('that target type is not valid')),
('RATELIMIT', _('you are doing that too much. try again in %(time)s.')),
('QUOTA_FILLED', _("You've submitted too many links recently. Please try again in an hour.")),
('SUBREDDIT_RATELIMIT', _("you are doing that too much. try again later.")),
('EXPIRED', _('your session has expired')),
('DRACONIAN', _('you must accept the terms first')),
('BANNED_IP', "IP banned"),
('BAD_CNAME', "that domain isn't going to work"),
('USED_CNAME', "that domain is already in use"),
('INVALID_OPTION', _('that option is not valid')),
('BAD_EMAIL', _('that email is invalid')),
('BAD_EMAILS', _('the following emails are invalid: %(emails)s')),
('NO_EMAILS', _('please enter at least one email address')),
('TOO_MANY_EMAILS', _('please only share to %(num)s emails at a time.')),
('NEWSLETTER_NO_EMAIL', _('where should we send that weekly newsletter?')),
('NEWSLETTER_EMAIL_UNACCEPTABLE', _('That email could not be added. Check your email for an existing confirmation email.')),
('OVERSOLD', _('that subreddit has already been oversold on %(start)s to %(end)s. Please pick another subreddit or date.')),
('OVERSOLD_DETAIL', _("We have insufficient inventory to fulfill your requested budget, target, and dates. Only %(available)s impressions available on %(target)s from %(start)s to %(end)s.")),
('BAD_DATE', _('please provide a date of the form mm/dd/yyyy')),
('BAD_DATE_RANGE', _('the dates need to be in order and not identical')),
('DATE_TOO_LATE', _('please enter a date %(day)s or earlier')),
('DATE_TOO_EARLY', _('please enter a date %(day)s or later')),
('BAD_ADDRESS', _('address problem: %(message)s')),
('BAD_CARD', _('card problem: %(message)s')),
('TOO_LONG', _("this is too long (max: %(max_length)s)")),
('NO_TEXT', _('we need something here')),
('TOO_SHORT', _("this is too short (min: %(min_length)s)")),
('INVALID_CODE', _("we've never seen that code before")),
('CLAIMED_CODE', _("that code has already been claimed -- perhaps by you?")),
('NO_SELFS', _("that subreddit doesn't allow text posts")),
('NO_LINKS', _("that subreddit only allows text posts")),
('TOO_OLD', _("that's a piece of history now; it's too late to reply to it")),
('BAD_CSS_NAME', _('invalid css name')),
('BAD_CSS', _('invalid css')),
('BAD_COLOR', _('invalid color')),
('BAD_REVISION', _('invalid revision ID')),
('TOO_MUCH_FLAIR_CSS', _('too many flair css classes')),
('BAD_FLAIR_TARGET', _('not a valid flair target')),
('OAUTH2_INVALID_CLIENT', _('invalid client id')),
('OAUTH2_INVALID_REDIRECT_URI', _('invalid redirect_uri parameter')),
('OAUTH2_INVALID_SCOPE', _('invalid scope requested')),
('OAUTH2_INVALID_REFRESH_TOKEN', _('invalid refresh token')),
('OAUTH2_ACCESS_DENIED', _('access denied by the user')),
('CONFIRM', _("please confirm the form")),
('CONFLICT', _("conflict error while saving")),
('NO_API', _('cannot perform this action via the API')),
('DOMAIN_BANNED', _('%(domain)s is not allowed on reddit: %(reason)s')),
('NO_OTP_SECRET', _('you must enable two-factor authentication')),
('OTP_ALREADY_ENABLED', _('two-factor authentication is already enabled')),
('BAD_IMAGE', _('image problem')),
('DEVELOPER_ALREADY_ADDED', _('already added')),
('TOO_MANY_DEVELOPERS', _('too many developers')),
('BAD_HASH', _("i don't believe you.")),
('ALREADY_MODERATOR', _('that user is already a moderator')),
('NO_INVITE_FOUND', _('there is no pending invite for that subreddit')),
('BID_LIVE', _('you cannot edit the budget of a live ad')),
('TOO_MANY_CAMPAIGNS', _('you have too many campaigns for that promotion')),
('BAD_JSONP_CALLBACK', _('that jsonp callback contains invalid characters')),
('INVALID_PERMISSION_TYPE', _("permissions don't apply to that type of user")),
('INVALID_PERMISSIONS', _('invalid permissions string')),
('BAD_MULTI_PATH', _('invalid multi path')),
('BAD_MULTI_NAME', _('%(reason)s')),
('MULTI_NOT_FOUND', _('that multireddit doesn\'t exist')),
('MULTI_EXISTS', _('that multireddit already exists')),
('MULTI_CANNOT_EDIT', _('you can\'t change that multireddit')),
('MULTI_TOO_MANY_SUBREDDITS', _('no more space for subreddits in that multireddit')),
('MULTI_SPECIAL_SUBREDDIT', _("can't add special subreddit %(path)s")),
('TOO_MANY_SUBREDDITS', _('maximum %(max)s subreddits')),
('JSON_PARSE_ERROR', _('unable to parse JSON data')),
('JSON_INVALID', _('unexpected JSON structure')),
('JSON_MISSING_KEY', _('JSON missing key: "%(key)s"')),
('NO_CHANGE_KIND', _("can't change post type")),
('INVALID_LOCATION', _("invalid location")),
('BANNED_FROM_SUBREDDIT', _('that user is banned from the subreddit')),
('GOLD_REQUIRED', _('you must have an active reddit gold subscription to do that')),
('INSUFFICIENT_CREDDITS', _("insufficient creddits")),
('SCRAPER_ERROR', _("unable to scrape provided url")),
('NO_SR_TO_SR_MESSAGE', _("can't send a message from a subreddit to another subreddit")),
('USER_BLOCKED_MESSAGE', _("can't send message to that user")),
('ADMIN_REQUIRED', _("you must be in admin mode for this")),
('CANT_CONVERT_TO_GOLD_ONLY', _("to convert an existing subreddit to gold only, send a message to %(admin_modmail)s")
% dict(admin_modmail=g.admin_message_acct)),
('GOLD_ONLY_SR_REQUIRED', _("this subreddit must be 'gold only' to select this")),
('CANT_CREATE_SR', _("your account is too new to create a subreddit. please contact the admins to request an exemption.")),
))
errors = Storage([(e, e) for e in error_list.keys()])
def add_error_codes(new_codes):
"""Add error codes to the error enumeration.
It is assumed that the incoming messages are marked for translation but not
yet translated, so they can be declared before pylons.i18n is ready.
"""
for code, message in new_codes.iteritems():
error_list[code] = _(message)
errors[code] = code
class RedditError(Exception):
name = None
fields = None
code = None
def __init__(self, name=None, msg_params=None, fields=None, code=None):
Exception.__init__(self)
if name is not None:
self.name = name
self.i18n_message = error_list.get(self.name)
self.msg_params = msg_params or {}
if fields is not None:
# list of fields in the original form that caused the error
self.fields = tup(fields)
if code is not None:
self.code = code
@property
def message(self):
return _(self.i18n_message) % self.msg_params
def __iter__(self):
yield ('name', self.name)
yield ('message', _(self.message))
def __repr__(self):
return '<RedditError: %s>' % self.name
def __str__(self):
return repr(self)
class ErrorSet(object):
def __init__(self):
self.errors = {}
def __contains__(self, pair):
"""Expects an (error_name, field_name) tuple and checks to
see if it's in the errors list."""
return self.errors.has_key(pair)
def get(self, name, default=None):
return self.errors.get(name, default)
def get_first(self, field_name, *error_names):
error = None
for error_name in error_names:
error = self.get((error_name, field_name))
if error:
return error
def __getitem__(self, name):
return self.errors[name]
def __repr__(self):
return "<ErrorSet %s>" % list(self)
def __iter__(self):
for x in self.errors:
yield x
def __len__(self):
return len(self.errors)
def add(self, error_name, msg_params=None, field=None, code=None):
for field_name in tup(field):
e = RedditError(error_name, msg_params, fields=field_name,
code=code)
self.add_error(e)
def add_error(self, error):
for field_name in tup(error.fields):
self.errors[(error.name, field_name)] = error
def remove(self, pair):
"""Expects an (error_name, field_name) tuple and removes it
from the errors list."""
if self.errors.has_key(pair):
del self.errors[pair]
class ForbiddenError(HTTPForbidden):
def __init__(self, error_name):
HTTPForbidden.__init__(self)
self.explanation = error_list[error_name]
class BadRequestError(HTTPBadRequest):
def __init__(self, error_name):
HTTPBadRequest.__init__(self)
self.error_data = {
'reason': error_name,
'explanation': error_list[error_name],
}
def reddit_http_error(code=400, error_name='UNKNOWN_ERROR', **data):
exc = status_map[code]()
data['reason'] = exc.explanation = error_name
if 'explanation' not in data and error_name in error_list:
data['explanation'] = exc.explanation = error_list[error_name]
# omit 'fields' json attribute if it is empty
if 'fields' in data and not data['fields']:
del data['fields']
exc.error_data = data
return exc
class UserRequiredException(RedditError):
name = errors.USER_REQUIRED
code = 403
class VerifiedUserRequiredException(RedditError):
name = errors.VERIFIED_USER_REQUIRED
code = 403
class MessageError(Exception): pass