Skip to content
Browse files

Raise mentos errors in Rubyland

  • Loading branch information...
1 parent 75ece4b commit 671d395e2568e4cb01358a5789100644b36bd4e0 @tnm committed Jul 9, 2012
Showing with 35 additions and 17 deletions.
  1. +25 −17 lib/pygments/mentos.py
  2. +10 −0 lib/pygments/popen.rb
View
42 lib/pygments/mentos.py
@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
import sys, re, os, signal
-import sys
+import traceback
if 'PYGMENTS_PATH' in os.environ:
sys.path.insert(0, os.environ['PYGMENTS_PATH'])
sys.path.append(os.getcwd() + "/vendor/")
@@ -102,6 +102,9 @@ def highlight_text(self, code, formatter_name, args, kwargs):
return res
+ else:
+ return {"error": "No lexer"}
+
def get_data(self, method, args, kwargs, text=None):
"""
Based on the method argument, determine the action we'd like pygments
@@ -190,25 +193,30 @@ def start(self):
header = None
if header:
- method = header["method"]
+ try:
+ method = header["method"]
+
+ # Default to empty array and empty dictionary if nothing is given. Default to
+ # no text and no further bytes to read.
+ args = header.get("args", [])
+ kwargs = header.get("kwargs", {})
+ text = ""
+ _bytes = 0
- # Default to empty array and empty dictionary if nothing is given. Default to
- # no text and no further bytes to read.
- args = header.get("args", [])
- kwargs = header.get("kwargs", {})
- text = ""
- _bytes = 0
+ # Check if we need to read additional bytes after the header.
+ _kwargs = header.get("kwargs", None)
+ if _kwargs:
+ _bytes = _kwargs.get("bytes", 0)
- # Check if we need to read additional bytes after the header.
- _kwargs = header.get("kwargs", None)
- if _kwargs:
- _bytes = _kwargs.get("bytes", 0)
+ # Read up to the given number bytes (possibly 0)
+ text = sys.stdin.read(_bytes)
- # Read up to the given number bytes (possibly 0)
- text = sys.stdin.read(_bytes)
+ # And now get the actual data from pygments.
+ res = self.get_data(method, args, kwargs, text)
- # And now get the actual data from pygments.
- res = self.get_data(method, args, kwargs, text)
+ except:
+ tb = traceback.format_exc()
+ res = {"error": tb}
# We return a header back to Rubyland also. If we don't have a result,
# we need to send back some 'error json' in the header.
@@ -227,7 +235,7 @@ def start(self):
header += res
if error == False:
- # The size of the reponse, including a newline.
+ # The size of the response, including a newline.
res_bytes = len(res) + 1
header["bytes"] = res_bytes
View
10 lib/pygments/popen.rb
@@ -224,11 +224,21 @@ def mentos(method, args=[], kwargs={}, code=nil)
# Get the response header
header = @out.gets
+
if header
# The header comes in as JSON
header = Yajl.load(header)
+
bytes = header["bytes"]
+ # Check if the header indicates an error
+ if header["error"]
+ # Raise this as a Ruby exception of the MentosError class.
+ # Pythonland will return a traceback, or at least some information
+ # about the error, in the error key.
+ raise MentosError.new(header["error"])
+ end
+
# Read more bytes (the actual response body)
res = @out.read(bytes.to_i)

0 comments on commit 671d395

Please sign in to comment.
Something went wrong with that request. Please try again.