Skip to content
This repository was archived by the owner on Jan 1, 2021. It is now read-only.
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 execjs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
'''
from __future__ import unicode_literals, division, with_statement

from execjs._exceptions import Error, RuntimeError, ProgramError, RuntimeUnavailableError
from execjs._exceptions import (
Error,
RuntimeError,
ProgramError,
RuntimeUnavailableError,
)

import execjs._runtimes
from execjs._external_runtime import ExternalRuntime
from execjs._abstract_runtime import AbstractRuntime
Expand Down
20 changes: 13 additions & 7 deletions execjs/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Abstract base error classes
class Error(Exception):
pass


# Abstract class that represents errors of runtime engine.
# ex. Specified runtime engine is not installed, runtime engine aborted (by its bugs).
# By the way "RuntimeError" is bad name because it is confusing with the standard exception.
class RuntimeError(Error):
pass

# Concrete runtime error classes
class RuntimeUnavailableError(RuntimeError): pass

class ProcessExitedWithNonZeroStatus(RuntimeError):
def __init__(self, status, stdout, stderr):
Error.__init__(self, status, stdout, stderr)
RuntimeError.__init__(self, status, stdout, stderr)
self.status = status
self.stdout = stdout
self.stderr = stderr


# Errors due to JS script.
# ex. Script has syntax error, executed and raised exception.
class ProgramError(Error):
pass


class RuntimeUnavailableError(RuntimeError):
pass
22 changes: 12 additions & 10 deletions execjs/_external_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import six
import execjs._json2 as _json2
import execjs._runner_sources as _runner_sources
import execjs._exceptions as exceptions

from execjs._exceptions import (
ProcessExitedWithNonZeroStatus,
ProgramError
)

from execjs._abstract_runtime import AbstractRuntime
from execjs._abstract_runtime_context import AbstractRuntimeContext
from execjs._misc import encode_unicode_codepoints
Expand Down Expand Up @@ -126,7 +131,7 @@ def _exec_with_tempfile(self, source):

def _fail_on_non_zero_status(self, status, stdoutdata, stderrdata):
if status != 0:
raise exceptions.RuntimeError(status=status, stdout=stdoutdata, stderr=stderrdata)
raise ProcessExitedWithNonZeroStatus(status=status, stdout=stdoutdata, stderr=stderrdata)

def _compile(self, source):
runner_source = self._runtime._runner_source
Expand All @@ -151,18 +156,15 @@ def _extract_result(self, output):
output = output.replace("\r\n", "\n").replace("\r", "\n")
output_last_line = output.split("\n")[-2]

if not output_last_line:
raise exceptions.ProgramError
else:
ret = json.loads(output_last_line)
if len(ret) == 1:
ret = [ret[0], None]
status, value = ret
ret = json.loads(output_last_line)
if len(ret) == 1:
ret = [ret[0], None]
status, value = ret

if status == "ok":
return value
else:
raise exceptions.ProgramError(value)
raise ProgramError(value)


def _is_windows():
Expand Down
2 changes: 1 addition & 1 deletion execjs/_pyv8runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _exec_(self, source):
try:
script = engine.compile(source)
except js_errors as e:
raise exceptions.RuntimeError(e)
raise exceptions.ProgramError(e)
try:
value = script.run()
except js_errors as e:
Expand Down