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.
 
 
 

48 lines
1.3 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. ErrInvalidData = errors.New("Invalid data.")
  14. ErrWorkWarning = errors.New("Work warning.")
  15. ErrWorkFail = errors.New("Work fail.")
  16. ErrWorkException = errors.New("Work exeption.")
  17. ErrDataType = errors.New("Invalid data type.")
  18. ErrOutOfCap = errors.New("Out of the capability.")
  19. ErrNotConn = errors.New("Did not connect to job server.")
  20. ErrFuncNotFound = errors.New("The function was not found.")
  21. )
  22. // Extract the error message
  23. func GetError(data []byte) (eno syscall.Errno, err error) {
  24. rel := bytes.SplitN(data, []byte{'\x00'}, 2)
  25. if len(rel) != 2 {
  26. err = Errorf("Not a error data: %V", data)
  27. return
  28. }
  29. l := len(rel[0])
  30. eno = syscall.Errno(BytesToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l-1]}))
  31. err = errors.New(string(rel[1]))
  32. return
  33. }
  34. // Get a formated error
  35. func Errorf(format string, msg ... interface{}) error {
  36. return errors.New(fmt.Sprintf(format, msg ... ))
  37. }
  38. // An error handler
  39. type ErrorHandler func(error)