Created
July 30, 2020 13:28
-
-
Save maxwellgithinji/0e803b261d680927837a7ae63d64109b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"log" | |
"os" | |
) | |
func main() { | |
f, err := os.Open("rumi.txt") | |
var perr *os.PathError | |
switch { | |
case errors.Is(err, os.ErrPermission) && errors.As(err, &perr): | |
err = fmt.Errorf("you do not have permission to open the file: %w and the path is %s", err, perr.Path) | |
log.Println(err) | |
case errors.Is(err, os.ErrNotExist) && errors.As(err, &perr): | |
err = fmt.Errorf("the file does not exist: %w and the path is %s", err, perr.Path) | |
log.Println(err) | |
case errors.As(err, &perr): | |
err = fmt.Errorf("here is the orignial error %w and the path %s", err, perr.Path) | |
log.Println(err) | |
case err != nil: | |
err = fmt.Errorf("file couldn't be opened: %w", err) | |
log.Println(err) | |
} | |
/* | |
if errors.Is(err, os.ErrPermission) && errors.As(err, &perr) { | |
err = fmt.Errorf("you do not have permission to open the file: %w and the path is %s", err, perr.Path) | |
log.Println(err) | |
} else if errors.Is(err, os.ErrNotExist) && errors.As(err, &perr) { | |
err = fmt.Errorf("the file does not exist: %w and the path is %s", err, perr.Path) | |
log.Println(err) | |
} else if errors.As(err, &perr) { | |
// return fmt.Errorf("here is the original error %w and the path %s", err, perr.Path) // RETURNS A ERROR | |
err = fmt.Errorf("here is the orignial error %w and the path %s", err, perr.Path) | |
log.Println(err) | |
} else if err != nil { | |
err = fmt.Errorf("file couldn't be opened: %w", err) | |
log.Println(err) | |
} | |
*/ | |
defer f.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment