functions.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Auxiliary functions which I'm always using.
  2. // Moved to separate file on 20211102.
  3. package main
  4. import (
  5. "fmt"
  6. "net/http"
  7. "path/filepath"
  8. "runtime"
  9. "github.com/google/uuid"
  10. // "github.com/op/go-logging"
  11. )
  12. // checkErrPanic logs a fatal error and panics.
  13. func checkErrPanic(err error) {
  14. if err != nil {
  15. pc, file, line, ok := runtime.Caller(1)
  16. log.Panicf("%s:%d (%v) [ok: %v] - panic: %v\n", filepath.Base(file), line, pc, ok, err)
  17. }
  18. }
  19. // checkErr checks if there is an error, and if yes, it logs it out and continues.
  20. //
  21. // this is for 'normal' situations when we want to get a log if something goes wrong but do not need to panic
  22. func checkErr(err error) {
  23. if err != nil {
  24. pc, file, line, ok := runtime.Caller(1)
  25. // log.Error(filepath.Base(file), ":", line, ":", pc, ok, " - error:", err)
  26. log.Errorf("%s:%d (%v) [ok: %v] - error: %v\n", filepath.Base(file), line, pc, ok, err)
  27. }
  28. }
  29. // Auxiliary functions for HTTP handling
  30. // checkErrHTTP returns an error via HTTP and also logs the error.
  31. func checkErrHTTP(w http.ResponseWriter, httpStatus int, errorMessage string, err error) {
  32. if err != nil {
  33. http.Error(w, fmt.Sprintf(errorMessage, err), httpStatus)
  34. pc, file, line, ok := runtime.Caller(1)
  35. log.Error("(", http.StatusText(httpStatus), ") ", filepath.Base(file), ":", line, ":", pc, ok, " - error:", errorMessage, err)
  36. }
  37. }
  38. // checkErrPanicHTTP returns an error via HTTP and logs the error with a panic.
  39. func checkErrPanicHTTP(w http.ResponseWriter, httpStatus int, errorMessage string, err error) {
  40. if err != nil {
  41. http.Error(w, fmt.Sprintf(errorMessage, err), httpStatus)
  42. pc, file, line, ok := runtime.Caller(1)
  43. log.Panic("(", http.StatusText(httpStatus), ") ", filepath.Base(file), ":", line, ":", pc, ok, " - panic:", errorMessage, err)
  44. }
  45. }
  46. // logErrHTTP assumes that the error message was already composed and writes it to HTTP and logs it.
  47. //
  48. // this is mostly to avoid code duplication and make sure that all entries are written similarly
  49. func logErrHTTP(w http.ResponseWriter, httpStatus int, errorMessage string) {
  50. http.Error(w, errorMessage, httpStatus)
  51. log.Error("(" + http.StatusText(httpStatus) + ") " + errorMessage)
  52. }
  53. // funcName is @Sonia's solution to get the name of the function that Go is currently running.
  54. //
  55. // This will be extensively used to deal with figuring out where in the code the errors are!
  56. // Source: https://stackoverflow.com/a/10743805/1035977 (20170708)
  57. func funcName() string {
  58. pc, _, _, _ := runtime.Caller(1)
  59. return runtime.FuncForPC(pc).Name()
  60. }
  61. // isValidUUID checks if the UUID is valid.
  62. // Thanks to Patrick D'Appollonio https://stackoverflow.com/questions/25051675/how-to-validate-uuid-v4-in-go
  63. // as well as https://stackoverflow.com/a/46315070/1035977 (gwyneth 29211031)
  64. /*
  65. // Deprecated, since regexps may be overkill here; Google's own package is far more efficient and we'll use it directly (gwyneth 20211031)
  66. func isValidUUID(uuid string) bool {
  67. r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  68. return r.MatchString(uuid)
  69. }
  70. */
  71. func isValidUUID(u string) bool {
  72. _, err := uuid.Parse(u)
  73. return err == nil
  74. }