This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1155 from vania-pooh/master
Switched to w3c standard compatible errors (fixes #1143)
- Loading branch information
Showing
4 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jsonerror | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type SeleniumError struct { | ||
Name string | ||
Err error | ||
Status int | ||
} | ||
|
||
func (se *SeleniumError) Error() string { | ||
return fmt.Errorf("%s: %v", se.Name, se.Err).Error() | ||
} | ||
|
||
func newSeleniumError(name string, err error, status int) *SeleniumError { | ||
return &SeleniumError{ | ||
Name: name, | ||
Err: err, | ||
Status: status, | ||
} | ||
} | ||
|
||
func InvalidArgument(err error) *SeleniumError { | ||
return newSeleniumError("invalid argument", err, http.StatusBadRequest) | ||
} | ||
|
||
func InvalidSessionID(err error) *SeleniumError { | ||
return newSeleniumError("invalid session id", err, http.StatusNotFound) | ||
} | ||
|
||
func SessionNotCreated(err error) *SeleniumError { | ||
return newSeleniumError("session not created", err, http.StatusInternalServerError) | ||
} | ||
|
||
func UnknownError(err error) *SeleniumError { | ||
return newSeleniumError("unknown error", err, http.StatusInternalServerError) | ||
} | ||
|
||
func (se *SeleniumError) Encode(w http.ResponseWriter) { | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.WriteHeader(se.Status) | ||
_ = json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"value": map[string]string{ | ||
"error": se.Name, | ||
"message": se.Err.Error(), | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters