return errors from jail

This commit is contained in:
Roman Volosovskyi 2016-06-22 15:46:00 +03:00
parent df47de9aea
commit 7b795df475
1 changed files with 26 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
"fmt"
) )
var statusJs string var statusJs string
@ -11,25 +12,45 @@ func Init(js string) {
statusJs = js statusJs = js
} }
func printResult(res string, err error) string {
var out string
if err != nil {
out = fmt.Sprintf(`{
"error": %s
}`,
err.Error())
} else {
out = fmt.Sprintf(`{
"result": %s
}`,
res)
}
return out
}
func Parse(chatId string, js string) string { func Parse(chatId string, js string) string {
vm := otto.New() vm := otto.New()
jjs := statusJs + js + ` jjs := statusJs + js + `
var catalog = JSON.stringify(_status_catalog); var catalog = JSON.stringify(_status_catalog);
` `
vms[chatId] = vm vms[chatId] = vm
vm.Run(jjs) _, err := vm.Run(jjs)
res, _ := vm.Get("catalog") res, _ := vm.Get("catalog")
err.Error()
return res.String() return printResult(res.String(), err)
} }
func Call(chatId string, path string, args string) string { func Call(chatId string, path string, args string) string {
vm, ok := vms[chatId] vm, ok := vms[chatId]
if !ok { if !ok {
return "" return fmt.Sprintf(
"{\"error\":\"Vm[%s] doesn't exist.\"}",
chatId)
} }
res, _ := vm.Call("call", nil, path, args) res, err := vm.Call("call", nil, path, args)
return res.String() return printResult(res.String(), err);
} }