Selaa lähdekoodia

Edited ServerModule1 and Form1

florian.residori@gmail.com 3 vuotta sitten
vanhempi
commit
162f7389dd
2 muutettua tiedostoa jossa 14 lisäystä ja 14 poistoa
  1. 5 1
      client_code/Form1/__init__.py
  2. 9 13
      server_code/ServerModule1.py

+ 5 - 1
client_code/Form1/__init__.py

@@ -24,7 +24,11 @@ class Form1(Form1Template):
 
   def check_json_click(self, **event_args):
     """This method is called when the button is clicked"""
-    parsed_data = anvil.server.call('parse_json',self.input.text)
+    status, parsed_data = anvil.server.call('parse_json',self.input.text)
+    if status==1:
+      self.output.foreground = 'red'
+    else:
+      self.output.foreground = 'blue'
     self.output.text = parsed_data
 
 

+ 9 - 13
server_code/ServerModule1.py

@@ -36,18 +36,14 @@ def parse_yaml(yaml_data):
 @anvil.server.callable
 def parse_json(json_data):
   try:
-    return pprint.pformat(json.loads(json_data), indent=2)
+    return (0,pprint.pformat(json.loads(json_data), indent=2))
   except JSONDecodeError as exc:
-    print(json_data)
-    if hasattr(exc, 'problem_mark'):
-        if exc.context != None:
-            error_message = ('Error while parsing YAML file:\n'+
-                              'parser says\n' + str(exc.problem_mark) + '\n  ' +
-                str(exc.problem) + ' ' + str(exc.context) +
-                '\nPlease correct data and retry.')
-        else:
-            error_message = ('Error while parsing YAML file:\n'+'  parser says\n' + str(exc.problem_mark) + '\n  ' +
-                str(exc.problem) + '\nPlease correct data and retry.')
-    else:
-        error_message =  ("Something went wrong while parsing yaml file")
+    error_message = ""
+    if exc.lineno - 1 >= 0:
+      error_message = error_message.join(json_data.splitlines()[exc.lineno - 3])
+    if exc.lineno >= 0:
+      error_message = error_message.join(json_data.splitlines()[exc.lineno - 2])
+    error_message = error_message.join(json_data.splitlines()[exc.lineno - 1])
+    if len(json_data.splitlines()) != exc.lineno:
+      error_message = error_message.join(json_data.splitlines()[exc.lineno])
     return (1,error_message)