WrapFileError takes an existing error and creates a new FileError for the given file.
(cause error, filename string)
| 64 | // WrapFileError takes an existing error and creates a new FileError for the |
| 65 | // given file. |
| 66 | func WrapFileError(cause error, filename string) *FileError { |
| 67 | if fileErr, ok := cause.(*FileError); ok { |
| 68 | return fileErr |
| 69 | } |
| 70 | var message string |
| 71 | switch err := cause.(type) { |
| 72 | case *os.PathError: |
| 73 | message = err.Err.Error() |
| 74 | case *os.LinkError: |
| 75 | message = err.Err.Error() |
| 76 | default: |
| 77 | message = cause.Error() |
| 78 | } |
| 79 | return &FileError{ |
| 80 | Message: message, |
| 81 | Filename: filename, |
| 82 | cause: cause, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func (err *FileError) Error() string { |
| 87 | return fmt.Sprintf("%s: %s", err.Filename, err.Message) |