error.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package ole
  2. // OleError stores COM errors.
  3. type OleError struct {
  4. hr uintptr
  5. description string
  6. subError error
  7. }
  8. // NewError creates new error with HResult.
  9. func NewError(hr uintptr) *OleError {
  10. return &OleError{hr: hr}
  11. }
  12. // NewErrorWithDescription creates new COM error with HResult and description.
  13. func NewErrorWithDescription(hr uintptr, description string) *OleError {
  14. return &OleError{hr: hr, description: description}
  15. }
  16. // NewErrorWithSubError creates new COM error with parent error.
  17. func NewErrorWithSubError(hr uintptr, description string, err error) *OleError {
  18. return &OleError{hr: hr, description: description, subError: err}
  19. }
  20. // Code is the HResult.
  21. func (v *OleError) Code() uintptr {
  22. return uintptr(v.hr)
  23. }
  24. // String description, either manually set or format message with error code.
  25. func (v *OleError) String() string {
  26. if v.description != "" {
  27. return errstr(int(v.hr)) + " (" + v.description + ")"
  28. }
  29. return errstr(int(v.hr))
  30. }
  31. // Error implements error interface.
  32. func (v *OleError) Error() string {
  33. return v.String()
  34. }
  35. // Description retrieves error summary, if there is one.
  36. func (v *OleError) Description() string {
  37. return v.description
  38. }
  39. // SubError returns parent error, if there is one.
  40. func (v *OleError) SubError() error {
  41. return v.subError
  42. }