Skip to content
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog

# 2.0.15
- Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3.

# 2.0.14
- Fixed inner module import path to use snake_case for exported dara filenames.

# 2.0.12
- Fixed the catch id error.

Expand Down Expand Up @@ -35,4 +41,4 @@
- Fixed the Inner Import & Export

# 2.0.0
- Upgrade By Darbaonba V2
- Upgrade By Darbaonba V2
5 changes: 4 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ChangeLog

# 2.0.15
- Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3.

# 2.0.14
- Fixed inner module import path to use snake_case for exported dara filenames.

Expand Down Expand Up @@ -38,4 +41,4 @@
- Fixed the Inner Import & Export

# 2.0.0
- Upgrade By Darbaonba V2
- Upgrade By Darbaonba V2
14 changes: 14 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,20 @@ setup(
const suffix = env.left ? ']' : ')';
var current = ast.id.inferred;

// Python's base Exception has no `message` attribute (removed in Py3).
// Untyped catch is emitted as `except Exception`, so `$Error.message`
// must not become `error.message`. Prefer DaraException.message when
// present, otherwise fall back to str(error).
if (!env.left
&& current
&& current.type === 'model'
&& current.name === '$Error'
&& ast.propertyPath.length === 1
&& _name(ast.propertyPath[0]) === 'message') {
this.emit(`getattr(${expr}, 'message', str(${expr}))`);
return;
}

for (var i = 0; i < ast.propertyPath.length; i++) {
var name = _name(ast.propertyPath[i]);
if (current.type === 'model') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@darabonba/python-generator",
"version": "2.0.14",
"version": "2.0.15",
"description": "The darabonba generator for Python",
"main": "lib/generator.js",
"directories": {
Expand Down
1 change: 1 addition & 0 deletions tests/expected/try/tea_python_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.0.0"
28 changes: 28 additions & 0 deletions tests/expected/try/tea_python_tests/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
from __future__ import annotations

from darabonba.exceptions import DaraException

class Client:

def __init__(self):
pass

@staticmethod
def wrap_error() -> None:
try :
raise DaraException({
'message': 'boom'
})
except Exception as error :
raise DaraException({
'message': getattr(error, 'message', str(error))
})


@staticmethod
def typed_error(
e: DaraException,
) -> str:
return getattr(e, 'message', str(e))
6 changes: 6 additions & 0 deletions tests/fixtures/try/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"scope": "alibabacloud",
"name": "main",
"version": "0.0.1",
"main": "./main.dara"
}
15 changes: 15 additions & 0 deletions tests/fixtures/try/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
static function wrapError(): void {
try {
throw {
message = "boom"
};
} catch(error) {
throw {
message = error.message
};
}
}

static function typedError(e: $Error): string {
return e.message;
}
10 changes: 10 additions & 0 deletions tests/main.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ describe('Python Generator', function () {
});
});

it('try $Error.message should ok', function () {
check('try', 'tea_python_tests',
{
python: {
package: 'tea_python_tests',
clientName: 'client'
}
});
});

it('function should ok', function () {
check('function', 'tea_python_tests',
{
Expand Down
Loading