Skip to content

Commit

Permalink
v1.3.0 because of http error codes fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Apr 28, 2019
1 parent 37924c5 commit 0f98d6c
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)

const version = "1.2.0"
const version = "1.3.0"

func main() {
err := cmd(os.Args[1:])
Expand Down Expand Up @@ -110,8 +110,11 @@ func initQueryHandler(dbPath string, queryString string, serverPort uint) (func(
if err == io.EOF || err == http.ErrBodyReadAfterClose /* last line is without \n */ {
break
} else if err != nil {
http.Error(w,
fmt.Sprintf("\n\nError reading request body: %v\n\n%s", err, helpMessage), http.StatusInternalServerError)
http.Error(
w,
fmt.Sprintf("\n\nError reading request body: %v\n\n%s", err, helpMessage),
http.StatusInternalServerError,
)
return
}

Expand All @@ -125,16 +128,22 @@ func initQueryHandler(dbPath string, queryString string, serverPort uint) (func(

rows, err := queryStmt.Query(queryParams...)
if err != nil {
msg := fmt.Sprintf("\n\nError executing query for params %#v: %v\n\n%s", csvLine, err, helpMessage)
http.Error(w, msg, http.StatusInternalServerError)
http.Error(
w,
fmt.Sprintf("\n\nError executing query for params %#v: %v\n\n%s", csvLine, err, helpMessage),
http.StatusInternalServerError,
)
return
}
defer rows.Close()

cols, err := rows.Columns()
if err != nil {
msg := fmt.Sprintf("\n\nError reading columns for query with params %#v: %v\n\n%s", csvLine, err, helpMessage)
http.Error(w, msg, http.StatusInternalServerError)
http.Error(
w,
fmt.Sprintf("\n\nError reading columns for query with params %#v: %v\n\n%s", csvLine, err, helpMessage),
http.StatusInternalServerError,
)
return
}

Expand All @@ -151,17 +160,23 @@ func initQueryHandler(dbPath string, queryString string, serverPort uint) (func(

err = rows.Scan(pointers...)
if err != nil {
http.Error(w,
fmt.Sprintf("\n\nError reading query results for params %#v: %v\n\n%s", csvLine, err, helpMessage), http.StatusInternalServerError)
http.Error(
w,
fmt.Sprintf("\n\nError reading query results for params %#v: %v\n\n%s", csvLine, err, helpMessage),
http.StatusInternalServerError,
)
return
}

queryAnswer.Out = append(queryAnswer.Out, row)
}
err = rows.Err()
if err != nil {
http.Error(w,
fmt.Sprintf("\n\nError executing query: %v\n\n%s", err, helpMessage), http.StatusInternalServerError)
http.Error(
w,
fmt.Sprintf("\n\nError executing query: %v\n\n%s", err, helpMessage),
http.StatusInternalServerError,
)
return
}

Expand All @@ -173,19 +188,21 @@ func initQueryHandler(dbPath string, queryString string, serverPort uint) (func(

answerJSON, err := json.Marshal(answer)
if err != nil {
http.Error(w,
http.Error(
w,
fmt.Sprintf("\n\nError encoding json: %v\n\n%s", err, helpMessage),
http.StatusInternalServerError)
http.StatusInternalServerError,
)
return
}

_, err = w.Write(answerJSON)
if err != nil {
log.Printf("Query: error 500 - cannot send json to client: %v\n", err)

http.Error(w,
http.Error(
w,
fmt.Sprintf("\n\nError sending json to client: %v\n\n%s", err, helpMessage),
http.StatusInternalServerError)
http.StatusInternalServerError,
)
return
}
}, nil
Expand Down

0 comments on commit 0f98d6c

Please sign in to comment.