71
package neterr
import (
"fmt"
"os"
)
func Fatal(v ...interface{}) {
fmt.Fprintln(os.Stderr, "manhttpd exited due to an error it could not recover from.")
fmt.Fprintf(os.Stderr, "Error: %s\n", fmt.Sprint(v...))
os.Exit(1)
}
var (
Err400 = HTCode(400, "Bad Request",
"Your request cannot be understood by the server.",
"Check that you are using a release version of manhttpd.",
"Please check spelling and try again.",
"Otherwise browser extensions or proxies may be hijacking your requests.",
)
Err404 = HTCode(404, "Not Found",
"The requested does match any known page names. Please check your spelling and try again.",
`If you cannot find the page using your system's man command, then you may need to update your manDB or apt-get <b><package>-doc</b>.`,
"If you can open a page using the cli but not in manhttpd, your service is misconfigured. For best results set user and group to your login user:",
`You can edit <<b>/etc/systemd/system/manhttpd.service</b>> and set "<b>User</b>=<<b>your-user</b>>" and "<b>Group</b>=<<b>your-group</b>>".`,
`Usually root user will work just fine, however root does not index user pages. If manuals are installed without superuser, they are saved to <<b>$HOME/.local/share/man/</b>>.`,
`If you want user pages you have to run manhttpd as your login user. If you really want to run the service as root with user directories, at your own risk: adding users' homes into the global path <<b>/etc/manpath.config</b>> is usually safe but may cause catastrophic failure on some systems.`,
)
Err500 = HTCode(500, "Internal Server Error",
"The server encountered an error and could not complete your request.",
"Make sure you are using a release version of manhttpd.",
)
)
func HTCode(code int, name string, desc ...string) NetErr {
return HTErr{code, name, desc}
}
func (h HTErr) Is(err error) bool {
if err == nil {
return false
}
return true
}
type HTErr struct {
Code int
Name string
Desc []string
}
type NetErr interface {
Error() HTErr
Is(error) bool
}
func (e HTErr) Error() HTErr {
return e
}
func (e HTErr) Title() string {
return fmt.Sprintf("%d %s", e.Code, e.Name)
}
func (e HTErr) Content() string {
s := fmt.Sprintf("<h1>%3d</h1><h2>%s</h2><br>\n", e.Code, e.Name)
for d := range e.Desc {
s += fmt.Sprintf("<p>%s</p><br>\n", e.Desc[d])
}
s += `<script>SetRawQuery()</script>
`
return s
}