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.
 
 
 

49 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. ErrEmptyReading = errors.New("Empty reading.")
  22. )
  23. // Extract the error message
  24. func GetError(data []byte) (eno syscall.Errno, err error) {
  25. rel := bytes.SplitN(data, []byte{'\x00'}, 2)
  26. if len(rel) != 2 {
  27. err = Errorf("Not a error data: %V", data)
  28. return
  29. }
  30. l := len(rel[0])
  31. eno = syscall.Errno(BytesToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l-1]}))
  32. err = errors.New(string(rel[1]))
  33. return
  34. }
  35. // Get a formated error
  36. func Errorf(format string, msg ... interface{}) error {
  37. return errors.New(fmt.Sprintf(format, msg ... ))
  38. }
  39. // An error handler
  40. type ErrorHandler func(error)