Skip to content

Commit b94e106

Browse files
committed
Improve error handling
1 parent f0e0ffc commit b94e106

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

autoload/vimpythondocstring.vim

+21-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,32 @@ sys.path[0:0] = deps
1212
import pydocstring
1313
EOF
1414

15+
function! s:handle_error(exception)
16+
echohl ErrorMsg
17+
echo join(map(split(a:exception, ":")[2:], 'trim(v:val)'), " : ")
18+
echohl None
19+
endfunction
20+
1521
function! vimpythondocstring#Full()
16-
python3 pydocstring.Docstring().full_docstring()
22+
try
23+
python3 pydocstring.Docstring().full_docstring()
24+
catch
25+
call s:handle_error(v:exception)
26+
endtry
1727
endfunction
1828

1929
function! vimpythondocstring#FullTypes()
20-
python3 pydocstring.Docstring().full_docstring(print_hints=True)
30+
try
31+
python3 pydocstring.Docstring().full_docstring(print_hints=True)
32+
catch
33+
call s:handle_error(v:exception)
34+
endtry
2135
endfunction
2236

2337
function! vimpythondocstring#Oneline()
24-
python3 pydocstring.Docstring().oneline_docstring()
38+
try
39+
python3 pydocstring.Docstring().oneline_docstring()
40+
catch
41+
call s:handle_error(v:exception)
42+
endtry
2543
endfunction

python/pydocstring.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/usr/bin/env python3
2-
from string import Template
3-
import re
4-
import os
5-
import ast
62
import abc
3+
import ast
4+
import os
5+
import re
6+
from string import Template
77

88
import ibis
9-
9+
from asthelper import ClassInstanceNameExtractor, ClassVisitor, MethodVisitor
1010
from utils import *
1111
from vimenv import *
12-
from asthelper import ClassVisitor, MethodVisitor, ClassInstanceNameExtractor
1312

1413

1514
class InvalidSyntax(Exception):
@@ -256,7 +255,10 @@ def __init__(self):
256255

257256
def _controller_factory(self, env, templater):
258257
line = env.current_line
259-
first_word = re.match(r"^\s*(\w+).*", line).groups()[0]
258+
try:
259+
first_word = re.match(r"^\s*(\w+).*", line).groups()[0]
260+
except Exception:
261+
first_word = None
260262
if first_word == "def":
261263
return MethodController(env, templater)
262264
elif first_word == "class":
@@ -268,18 +270,18 @@ def _controller_factory(self, env, templater):
268270
if second_word == "def":
269271
return MethodController(env, templater)
270272

271-
raise DocstringUnavailable("Docstring cannot be created for selected object")
273+
raise DocstringUnavailable("Docstring ERROR: Doctring cannot be created for selected object")
272274

273275
def full_docstring(self, print_hints=False):
274276
"""Writes docstring containing arguments, returns, raises, ..."""
275277
try:
276278
self.obj_controller.write_docstring(print_hints=print_hints)
277279
except Exception as e:
278-
print(concat_("Doctring ERROR: ", e))
280+
raise DocstringUnavailable(concat_("Docstring ERROR: ", e))
279281

280282
def oneline_docstring(self):
281283
"""Writes only a one-line empty docstring"""
282284
try:
283285
self.obj_controller.write_simple_docstring()
284286
except Exception as e:
285-
print(concat_("Doctring ERROR: ", e))
287+
raise DocstringUnavailable(concat_("Docstring ERROR: ", e))

0 commit comments

Comments
 (0)