Skip to content

Commit 0611696

Browse files
committed
errors: use the documentation as the error message, if available
1 parent 40057e2 commit 0611696

File tree

13 files changed

+1002
-144
lines changed

13 files changed

+1002
-144
lines changed

generator/rust.stoneg.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,54 @@ def _impl_error(self, typ):
922922
u'fmt',
923923
[u'&self', u'f: &mut ::std::fmt::Formatter<\'_>'],
924924
u'::std::fmt::Result'):
925-
self.emit(u'write!(f, "{:?}", *self)')
925+
926+
# Find variants that have documentation and use the first line for the Display
927+
# representation of the error.
928+
doc_variants = []
929+
for variant in variants:
930+
if variant.doc:
931+
line = variant.doc.split('\n')[0]
932+
# If the line has doc references, it's not going to make a good display
933+
# string, so only include it if it has none:
934+
if line == self.process_doc(line, lambda tag, value: ''):
935+
doc_variants.append((variant, line))
936+
else:
937+
# TODO(wfraser): don't reject it outright; see if it has an inner value.
938+
# It's pretty common to have no doc, but an inner error type that does.
939+
print("bad doc for " + type_name + "::" + variant.name)
940+
941+
if doc_variants:
942+
with self.block(u'match self'):
943+
for variant, line in doc_variants:
944+
var_exp = u'{}::{}'.format(type_name, self.enum_variant_name(variant))
945+
args = ""
946+
if self._is_error_type(variant.data_type):
947+
# Include the Display representation of the inner error.
948+
var_exp += u'(inner)'
949+
if line.endswith(u'.'):
950+
line = line[:-1]
951+
line += u': {}'
952+
args = u'inner'
953+
elif not ir.is_void_type(variant.data_type):
954+
# Include the Debug representation of the inner value.
955+
var_exp += u'(inner)'
956+
if line.endswith('.'):
957+
line = line[:-1]
958+
line += u': {:?}'
959+
args = u'inner'
960+
961+
if not args:
962+
self.emit(u'{} => f.write_str("{}"),'.format(var_exp, line))
963+
else:
964+
self.emit(u'{} => write!(f, "{}", {}),'.format(
965+
var_exp, line, args))
966+
967+
if not self.is_closed_union(typ) or len(doc_variants) != len(variants):
968+
# fall back on the Debug representation
969+
self.emit(u'_ => write!(f, "{:?}", *self),')
970+
else:
971+
# skip the whole match block and just use the Debug representation
972+
self.emit(u'write!(f, "{:?}", *self)')
926973
self.emit()
927974

928975
# Naming Rules

src/generated/account.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,14 @@ impl ::std::error::Error for SetProfilePhotoError {
290290

291291
impl ::std::fmt::Display for SetProfilePhotoError {
292292
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
293-
write!(f, "{:?}", *self)
293+
match self {
294+
SetProfilePhotoError::FileTypeError => f.write_str("File cannot be set as profile photo."),
295+
SetProfilePhotoError::FileSizeError => f.write_str("File cannot exceed 10 MB."),
296+
SetProfilePhotoError::DimensionError => f.write_str("Image must be larger than 128 x 128."),
297+
SetProfilePhotoError::ThumbnailError => f.write_str("Image could not be thumbnailed."),
298+
SetProfilePhotoError::TransientError => f.write_str("Temporary infrastructure failure, please retry."),
299+
_ => write!(f, "{:?}", *self),
300+
}
294301
}
295302
}
296303

src/generated/auth.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ impl ::std::error::Error for AccessError {
127127

128128
impl ::std::fmt::Display for AccessError {
129129
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
130-
write!(f, "{:?}", *self)
130+
match self {
131+
AccessError::InvalidAccountType(inner) => write!(f, "Current account type cannot access the resource: {}", inner),
132+
AccessError::PaperAccessDenied(inner) => write!(f, "Current account cannot access Paper: {}", inner),
133+
_ => write!(f, "{:?}", *self),
134+
}
131135
}
132136
}
133137

@@ -272,7 +276,16 @@ impl ::std::error::Error for AuthError {
272276

273277
impl ::std::fmt::Display for AuthError {
274278
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
275-
write!(f, "{:?}", *self)
279+
match self {
280+
AuthError::InvalidAccessToken => f.write_str("The access token is invalid."),
281+
AuthError::InvalidSelectUser => f.write_str("The user specified in 'Dropbox-API-Select-User' is no longer on the team."),
282+
AuthError::InvalidSelectAdmin => f.write_str("The user specified in 'Dropbox-API-Select-Admin' is not a Dropbox Business team admin."),
283+
AuthError::UserSuspended => f.write_str("The user has been suspended."),
284+
AuthError::ExpiredAccessToken => f.write_str("The access token has expired."),
285+
AuthError::MissingScope(inner) => write!(f, "The access token does not have the required scope to access the route: {:?}", inner),
286+
AuthError::RouteAccessDenied => f.write_str("The route is not available to public."),
287+
_ => write!(f, "{:?}", *self),
288+
}
276289
}
277290
}
278291

@@ -353,7 +366,11 @@ impl ::std::error::Error for InvalidAccountTypeError {
353366

354367
impl ::std::fmt::Display for InvalidAccountTypeError {
355368
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
356-
write!(f, "{:?}", *self)
369+
match self {
370+
InvalidAccountTypeError::Endpoint => f.write_str("Current account type doesn't have permission to access this route endpoint."),
371+
InvalidAccountTypeError::Feature => f.write_str("Current account type doesn't have permission to access this feature."),
372+
_ => write!(f, "{:?}", *self),
373+
}
357374
}
358375
}
359376

@@ -434,7 +451,11 @@ impl ::std::error::Error for PaperAccessError {
434451

435452
impl ::std::fmt::Display for PaperAccessError {
436453
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
437-
write!(f, "{:?}", *self)
454+
match self {
455+
PaperAccessError::PaperDisabled => f.write_str("Paper is disabled."),
456+
PaperAccessError::NotPaperUser => f.write_str("The provided user has not used Paper yet."),
457+
_ => write!(f, "{:?}", *self),
458+
}
438459
}
439460
}
440461

@@ -799,7 +820,11 @@ impl ::std::error::Error for TokenFromOAuth1Error {
799820

800821
impl ::std::fmt::Display for TokenFromOAuth1Error {
801822
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
802-
write!(f, "{:?}", *self)
823+
match self {
824+
TokenFromOAuth1Error::InvalidOauth1TokenInfo => f.write_str("Part or all of the OAuth 1.0 access token info is invalid."),
825+
TokenFromOAuth1Error::AppIdMismatch => f.write_str("The authorized app does not match the app associated with the supplied access token."),
826+
_ => write!(f, "{:?}", *self),
827+
}
803828
}
804829
}
805830

src/generated/common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ impl ::std::error::Error for PathRootError {
199199

200200
impl ::std::fmt::Display for PathRootError {
201201
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
202-
write!(f, "{:?}", *self)
202+
match self {
203+
PathRootError::InvalidRoot(inner) => write!(f, "The root namespace id in Dropbox-API-Path-Root header is not valid. The value of this error is use's latest root info: {:?}", inner),
204+
PathRootError::NoPermission => f.write_str("You don't have permission to access the namespace id in Dropbox-API-Path-Root header."),
205+
_ => write!(f, "{:?}", *self),
206+
}
203207
}
204208
}
205209

src/generated/contacts.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ impl ::std::error::Error for DeleteManualContactsError {
194194

195195
impl ::std::fmt::Display for DeleteManualContactsError {
196196
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
197-
write!(f, "{:?}", *self)
197+
match self {
198+
DeleteManualContactsError::ContactsNotFound(inner) => write!(f, "Can't delete contacts from this list. Make sure the list only has manually added contacts. The deletion was cancelled: {:?}", inner),
199+
_ => write!(f, "{:?}", *self),
200+
}
198201
}
199202
}
200203

src/generated/dbx_async.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ impl ::std::error::Error for PollError {
374374

375375
impl ::std::fmt::Display for PollError {
376376
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
377-
write!(f, "{:?}", *self)
377+
match self {
378+
PollError::InvalidAsyncJobId => f.write_str("The job ID is invalid."),
379+
PollError::InternalError => f.write_str("Something went wrong with the job on Dropbox's end. You'll need to verify that the action you were taking succeeded, and if not, try again. This should happen very rarely."),
380+
_ => write!(f, "{:?}", *self),
381+
}
378382
}
379383
}
380384

src/generated/file_properties.rs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,16 @@ impl ::std::error::Error for AddPropertiesError {
567567

568568
impl ::std::fmt::Display for AddPropertiesError {
569569
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
570-
write!(f, "{:?}", *self)
570+
match self {
571+
AddPropertiesError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
572+
AddPropertiesError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
573+
AddPropertiesError::UnsupportedFolder => f.write_str("This folder cannot be tagged. Tagging folders is not supported for team-owned templates."),
574+
AddPropertiesError::PropertyFieldTooLarge => f.write_str("One or more of the supplied property field values is too large."),
575+
AddPropertiesError::DoesNotFitTemplate => f.write_str("One or more of the supplied property fields does not conform to the template specifications."),
576+
AddPropertiesError::DuplicatePropertyGroups => f.write_str("There are 2 or more property groups referring to the same templates in the input."),
577+
AddPropertiesError::PropertyGroupAlreadyExists => f.write_str("A property group associated with this template and file already exists."),
578+
_ => write!(f, "{:?}", *self),
579+
}
571580
}
572581
}
573582

@@ -1144,7 +1153,15 @@ impl ::std::error::Error for InvalidPropertyGroupError {
11441153

11451154
impl ::std::fmt::Display for InvalidPropertyGroupError {
11461155
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
1147-
write!(f, "{:?}", *self)
1156+
match self {
1157+
InvalidPropertyGroupError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
1158+
InvalidPropertyGroupError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
1159+
InvalidPropertyGroupError::UnsupportedFolder => f.write_str("This folder cannot be tagged. Tagging folders is not supported for team-owned templates."),
1160+
InvalidPropertyGroupError::PropertyFieldTooLarge => f.write_str("One or more of the supplied property field values is too large."),
1161+
InvalidPropertyGroupError::DoesNotFitTemplate => f.write_str("One or more of the supplied property fields does not conform to the template specifications."),
1162+
InvalidPropertyGroupError::DuplicatePropertyGroups => f.write_str("There are 2 or more property groups referring to the same templates in the input."),
1163+
_ => write!(f, "{:?}", *self),
1164+
}
11481165
}
11491166
}
11501167

@@ -1364,7 +1381,10 @@ impl ::std::error::Error for LookUpPropertiesError {
13641381

13651382
impl ::std::fmt::Display for LookUpPropertiesError {
13661383
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
1367-
write!(f, "{:?}", *self)
1384+
match self {
1385+
LookUpPropertiesError::PropertyGroupNotFound => f.write_str("No property group was found."),
1386+
_ => write!(f, "{:?}", *self),
1387+
}
13681388
}
13691389
}
13701390

@@ -1488,7 +1508,13 @@ impl ::std::error::Error for LookupError {
14881508

14891509
impl ::std::fmt::Display for LookupError {
14901510
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
1491-
write!(f, "{:?}", *self)
1511+
match self {
1512+
LookupError::NotFound => f.write_str("There is nothing at the given path."),
1513+
LookupError::NotFile => f.write_str("We were expecting a file, but the given path refers to something that isn't a file."),
1514+
LookupError::NotFolder => f.write_str("We were expecting a folder, but the given path refers to something that isn't a folder."),
1515+
LookupError::RestrictedContent => f.write_str("The file cannot be transferred because the content is restricted. For example, sometimes there are legal restrictions due to copyright claims."),
1516+
_ => write!(f, "{:?}", *self),
1517+
}
14921518
}
14931519
}
14941520

@@ -1626,7 +1652,15 @@ impl ::std::error::Error for ModifyTemplateError {
16261652

16271653
impl ::std::fmt::Display for ModifyTemplateError {
16281654
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
1629-
write!(f, "{:?}", *self)
1655+
match self {
1656+
ModifyTemplateError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
1657+
ModifyTemplateError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
1658+
ModifyTemplateError::ConflictingPropertyNames => f.write_str("A property field key with that name already exists in the template."),
1659+
ModifyTemplateError::TooManyProperties => f.write_str("There are too many properties in the changed template. The maximum number of properties per template is 32."),
1660+
ModifyTemplateError::TooManyTemplates => f.write_str("There are too many templates for the team."),
1661+
ModifyTemplateError::TemplateAttributeTooLarge => f.write_str("The template name, description or one or more of the property field keys is too large."),
1662+
_ => write!(f, "{:?}", *self),
1663+
}
16301664
}
16311665
}
16321666

@@ -1850,7 +1884,12 @@ impl ::std::error::Error for PropertiesError {
18501884

18511885
impl ::std::fmt::Display for PropertiesError {
18521886
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
1853-
write!(f, "{:?}", *self)
1887+
match self {
1888+
PropertiesError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
1889+
PropertiesError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
1890+
PropertiesError::UnsupportedFolder => f.write_str("This folder cannot be tagged. Tagging folders is not supported for team-owned templates."),
1891+
_ => write!(f, "{:?}", *self),
1892+
}
18541893
}
18551894
}
18561895

@@ -3502,7 +3541,12 @@ impl ::std::error::Error for RemovePropertiesError {
35023541

35033542
impl ::std::fmt::Display for RemovePropertiesError {
35043543
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
3505-
write!(f, "{:?}", *self)
3544+
match self {
3545+
RemovePropertiesError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
3546+
RemovePropertiesError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
3547+
RemovePropertiesError::UnsupportedFolder => f.write_str("This folder cannot be tagged. Tagging folders is not supported for team-owned templates."),
3548+
_ => write!(f, "{:?}", *self),
3549+
}
35063550
}
35073551
}
35083552

@@ -3678,7 +3722,11 @@ impl ::std::error::Error for TemplateError {
36783722

36793723
impl ::std::fmt::Display for TemplateError {
36803724
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
3681-
write!(f, "{:?}", *self)
3725+
match self {
3726+
TemplateError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
3727+
TemplateError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
3728+
_ => write!(f, "{:?}", *self),
3729+
}
36823730
}
36833731
}
36843732

@@ -4170,7 +4218,15 @@ impl ::std::error::Error for UpdatePropertiesError {
41704218

41714219
impl ::std::fmt::Display for UpdatePropertiesError {
41724220
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
4173-
write!(f, "{:?}", *self)
4221+
match self {
4222+
UpdatePropertiesError::TemplateNotFound(inner) => write!(f, "Template does not exist for the given identifier: {:?}", inner),
4223+
UpdatePropertiesError::RestrictedContent => f.write_str("You do not have permission to modify this template."),
4224+
UpdatePropertiesError::UnsupportedFolder => f.write_str("This folder cannot be tagged. Tagging folders is not supported for team-owned templates."),
4225+
UpdatePropertiesError::PropertyFieldTooLarge => f.write_str("One or more of the supplied property field values is too large."),
4226+
UpdatePropertiesError::DoesNotFitTemplate => f.write_str("One or more of the supplied property fields does not conform to the template specifications."),
4227+
UpdatePropertiesError::DuplicatePropertyGroups => f.write_str("There are 2 or more property groups referring to the same templates in the input."),
4228+
_ => write!(f, "{:?}", *self),
4229+
}
41744230
}
41754231
}
41764232

0 commit comments

Comments
 (0)