added error handling

This commit is contained in:
Mark Joling 2018-10-10 12:47:20 +02:00 committed by GitHub
parent 8dcfea4b90
commit a14a810925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 101 additions and 92 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
@ -47,7 +48,9 @@ func main() {
fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil); if err != nil{
log.Fatal(err)
}
}
func Execute(res http.ResponseWriter, req *http.Request) {
@ -87,7 +90,9 @@ func Execute(res http.ResponseWriter, req *http.Request) {
Message: inbound.Language + " is not supported by Piston",
}
pres, _ := json.Marshal(problem)
pres, err := json.Marshal(problem); if err != nil{
log.Fatal(err)
}
res.Write(pres)
}
@ -121,7 +126,9 @@ func launch(request inbound, res http.ResponseWriter) {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
err := cmd.Run(); if err != nil{
log.Fatal(err)
}
// prepare response
outbound := outbound{
@ -130,7 +137,9 @@ func launch(request inbound, res http.ResponseWriter) {
Output: strings.TrimSpace(stdout.String()),
}
response, _ := json.Marshal(outbound)
response, err:= json.Marshal(outbound); if err != nil{
log.Fatal(err)
}
res.Write(response)
}