Simple change for the "error handling"

This commit is contained in:
2024-05-21 19:06:30 +01:00
parent 8a625b5fa8
commit 40ef1e3dc5
3 changed files with 23 additions and 10 deletions
+10 -2
View File
@@ -15,7 +15,7 @@
<button id="fax-submit-button" type="button">Submit</button>
<br>
<div id="req-result">-&gt;</div>
<div id="req-result"></div>
</form>
</div>
@@ -52,7 +52,15 @@
}
})
.then(data => {
console.log("Success:", data);
const response = JSON.parse(data)
console.log(data)
var elem = document.getElementById("req-result")
if (response["state"] === "success") {
elem.innerHTML = "-> Fax sent successfully <-"
} else {
elem.innerHTML = "-> Fax failed to send <-"
}
})
.catch(error => {
console.error(error);
+10 -8
View File
@@ -19,6 +19,10 @@ type WebFrontendRequest struct {
Message string `json:"fax_message"`
}
type WebFrontendResponse struct {
State string `json:"state"`
}
// SendRequestToPrinter function will request the thermal printer
// to print the message.
// It should be able to send the requests both via http/https or sockets
@@ -63,27 +67,25 @@ func SendFax(w http.ResponseWriter, r *http.Request) {
}
session, _ := models.GetSession(token)
log.Printf(" | User %s request a fax.", session.User)
log.Printf("| User %s requested a fax", session.User)
data, _ := io.ReadAll(r.Body)
log.Printf("| Received Request: %s", data)
// Parse the JSON data that comes from the form
var formData WebFrontendRequest
err := json.Unmarshal(data, &formData)
if err != nil {
log.Println("Failed to unmarshal form data: ", err)
w.Write([]byte("Failed to unmarshal form data."))
w.Write([]byte("{\"state\":\"failed\"}"))
return
}
resp, err := sendRequestToPrinter(formData.Message)
_, err = sendRequestToPrinter(formData.Message)
if err != nil {
w.Write([]byte(fmt.Sprintf("Failed to send message to printer: %s", err)))
w.Write([]byte("{\"state\":\"failed\"}"))
return
}
log.Println("Print Service Response: ", resp.Message)
w.Write([]byte("Success"))
w.Header().Set("Content-Type", "text/json")
w.Write([]byte("{\"state\":\"success\"}"))
}
+3
View File
@@ -42,6 +42,7 @@ func openDB() *Database {
return &d
}
// getUserPass will return the password (encrypted) of the user that is trying to log in
func getUserPass(user string) (string, error) {
d := openDB()
@@ -71,6 +72,7 @@ func getUserPass(user string) (string, error) {
return p, nil
}
// insertToken will insert the token generated by a successful authentication
func insertToken(token string, user string, expirationDate int64) error {
d := openDB()
@@ -93,6 +95,7 @@ func insertToken(token string, user string, expirationDate int64) error {
return nil
}
// getSessionData returns the data of a session for a given token
func getSessionData(token string) (*SessionData, error) {
d := openDB()