File: error.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (59 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package hdfs

import (
	"os"
	"syscall"
)

const (
	fileNotFoundException        = "java.io.FileNotFoundException"
	permissionDeniedException    = "org.apache.hadoop.security.AccessControlException"
	pathIsNotEmptyDirException   = "org.apache.hadoop.fs.PathIsNotEmptyDirectoryException"
	fileAlreadyExistsException   = "org.apache.hadoop.fs.FileAlreadyExistsException"
	alreadyBeingCreatedException = "org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException"
	illegalArgumentException     = "org.apache.hadoop.HadoopIllegalArgumentException"
)

// Error represents a remote java exception from an HDFS namenode or datanode.
type Error interface {
	// Method returns the RPC method that encountered an error.
	Method() string
	// Desc returns the long form of the error code (for example ERROR_CHECKSUM).
	Desc() string
	// Exception returns the java exception class name (for example
	// java.io.FileNotFoundException).
	Exception() string
	// Message returns the full error message, complete with java exception
	// traceback.
	Message() string
}

func interpretCreateException(err error) error {
	if remoteErr, ok := err.(Error); ok && remoteErr.Exception() == alreadyBeingCreatedException {
		return os.ErrExist
	}

	return interpretException(err)
}

func interpretException(err error) error {
	var exception string
	if remoteErr, ok := err.(Error); ok {
		exception = remoteErr.Exception()
	}

	switch exception {
	case fileNotFoundException:
		return os.ErrNotExist
	case permissionDeniedException:
		return os.ErrPermission
	case pathIsNotEmptyDirException:
		return syscall.ENOTEMPTY
	case fileAlreadyExistsException:
		return os.ErrExist
	case illegalArgumentException:
		return os.ErrInvalid
	default:
		return err
	}
}