You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.4 KiB

  1. // Copyright 2011 - 2012 Xing Xing <mikespook@gmail.com>.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT
  4. // license that can be found in the LICENSE file.
  5. package common
  6. import (
  7. "fmt"
  8. "bytes"
  9. "errors"
  10. "syscall"
  11. )
  12. var (
  13. ErrJobTimeOut = errors.New("Do a job time out.")
  14. ErrInvalidData = errors.New("Invalid data.")
  15. ErrWorkWarning = errors.New("Work warning.")
  16. ErrWorkFail = errors.New("Work fail.")
  17. ErrWorkException = errors.New("Work exeption.")
  18. ErrDataType = errors.New("Invalid data type.")
  19. ErrOutOfCap = errors.New("Out of the capability.")
  20. ErrNotConn = errors.New("Did not connect to job server.")
  21. ErrFuncNotFound = errors.New("The function was not found.")
  22. ErrEmptyReading = errors.New("Empty reading.")
  23. )
  24. func DisablePanic() {recover()}
  25. // Extract the error message
  26. func GetError(data []byte) (eno syscall.Errno, err error) {
  27. rel := bytes.SplitN(data, []byte{'\x00'}, 2)
  28. if len(rel) != 2 {
  29. err = Errorf("Not a error data: %V", data)
  30. return
  31. }
  32. l := len(rel[0])
  33. eno = syscall.Errno(BytesToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l-1]}))
  34. err = errors.New(string(rel[1]))
  35. return
  36. }
  37. // Get a formated error
  38. func Errorf(format string, msg ... interface{}) error {
  39. return errors.New(fmt.Sprintf(format, msg ... ))
  40. }
  41. // An error handler
  42. type ErrorHandler func(error)