Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error response wrapping #238

Merged
merged 8 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stone/backends/js_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
help=('Wraps the response in a response class')
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Remove extra line

_cmdline_parser.add_argument(
'--wrap-error-in',
type=str,
default='',
help=('Wraps the error in an error class')
)

_header = """\
// Auto-generated by Stone, do not modify.
var routes = {};
Expand Down Expand Up @@ -99,7 +106,7 @@ def _generate_route(self, route_schema, namespace, route):
fmt_type(route.arg_data_type))
self.emit(' * @returns {Promise.<%s, %s>}' %
(return_type,
fmt_error_type(route.error_data_type)))
fmt_error_type(route.error_data_type, self.args.wrap_error_in)))
self.emit(' */')

if route.arg_data_type.__class__ != Void:
Expand Down
7 changes: 5 additions & 2 deletions stone/backends/js_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ def fmt_obj(o):
return json.dumps(o, indent=2)


def fmt_error_type(data_type):
def fmt_error_type(data_type, wrap_error_in=''):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can't we just default this to wrap_error_in='Error'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need to default to '' becuase that is what comes in if the flag isn't set, so this would have to be checked when we call it and I think its better to abstract that here

"""
Converts the error type into a JSDoc type.
"""
return 'Error.<%s>' % fmt_type(data_type)
return '%s.<%s>' % (
(wrap_error_in if (wrap_error_in != '') else 'Error'),
fmt_type(data_type)
)


def fmt_type_name(data_type):
Expand Down
10 changes: 8 additions & 2 deletions stone/backends/tsd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@
default='',
help=('Wraps the response in a response class')
)

_cmdline_parser.add_argument(
'--wrap-error-in',
type=str,
default='',
help=('Wraps the error in an error class')
)
_cmdline_parser.add_argument(
'--import-namespaces',
default=False,
Expand Down Expand Up @@ -169,7 +174,8 @@ def _generate_route(self, namespace, route):
self.emit_wrapped_text(self.process_doc(route.doc, self._docf), prefix=' * ')
self.emit(' *')
self.emit_wrapped_text('When an error occurs, the route rejects the promise with type %s.'
% fmt_error_type(route.error_data_type), prefix=' * ')
% fmt_error_type(route.error_data_type,
self.args.wrap_error_in), prefix=' * ')
if route.deprecated:
self.emit(' * @deprecated')

Expand Down
7 changes: 5 additions & 2 deletions stone/backends/tsd_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@
}


def fmt_error_type(data_type, inside_namespace=None):
def fmt_error_type(data_type, inside_namespace=None, wrap_error_in=''):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Same here - just default to wrap_error_in='Error'

"""
Converts the error type into a TypeScript type.
inside_namespace should be set to the namespace that the reference
occurs in, or None if this parameter is not relevant.
"""
return 'Error<%s>' % fmt_type(data_type, inside_namespace)
return '%s<%s>' % (
(wrap_error_in if (wrap_error_in != '') else 'Error'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Same here - and then update to only wrap_error_in

fmt_type(data_type, inside_namespace)
)

def fmt_type_name(data_type, inside_namespace=None):
"""
Expand Down