2016-10-22 11:07:19 +00:00
|
|
|
package errors
|
2016-06-25 20:57:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2016-10-31 21:26:47 +00:00
|
|
|
// ErrorToHTTPCode converts errors to HTTP Status Code.
|
2016-10-18 20:30:10 +00:00
|
|
|
func ErrorToHTTPCode(err error, gone bool) int {
|
2016-06-25 20:57:10 +00:00
|
|
|
switch {
|
|
|
|
case os.IsPermission(err):
|
|
|
|
return http.StatusForbidden
|
|
|
|
case os.IsNotExist(err):
|
2016-10-18 20:30:10 +00:00
|
|
|
if !gone {
|
|
|
|
return http.StatusNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusGone
|
2016-06-25 20:57:10 +00:00
|
|
|
case os.IsExist(err):
|
|
|
|
return http.StatusGone
|
|
|
|
default:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
}
|