Passed
Push — main ( 9e2022...6ef1c9 )
by Christian
02:04 queued 14s
created

adapter.*oracleAdapter.ErrorHandler   C

Complexity

Conditions 11

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nop 1
dl 0
loc 26
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like adapter.*oracleAdapter.ErrorHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package adapter
2
3
import (
4
	"fmt"
5
6
	"github.com/cdleo/go-commons/sqlcommons"
7
	"github.com/godror/godror"
8
)
9
10
type oracleAdapter struct{}
11
12
func NewOracleAdapter() sqlcommons.SQLAdapter {
13
	return &oracleAdapter{}
14
}
15
16
func (t *oracleAdapter) Translate(query string) string {
17
	return query
18
}
19
20
func (s *oracleAdapter) ErrorHandler(err error) error {
21
	if err == nil {
22
		return nil
23
	}
24
25
	if oraError, ok := godror.AsOraErr(err); ok {
26
		switch oraError.Code() {
27
		case 1: //ORA-00001"
28
			return sqlcommons.UniqueConstraintViolation
29
		case 2291, 2292: //ORA-02291 (PKNotFound) AND ORA-02292 (ChildFound)
30
			return sqlcommons.IntegrityConstraintViolation
31
		case 12899: //ORA-12899
32
			return sqlcommons.ValueTooLargeForColumn
33
		case 1438: //ORA-01438
34
			return sqlcommons.ValueLargerThanPrecision
35
		case 1400, 1407: //ORA-01400 (cannot insert) AND ORA-01407 (cannot change value to)
36
			return sqlcommons.CannotSetNullColumn
37
		case 1722: //ORA-01722
38
			return sqlcommons.InvalidNumericValue
39
		case 1427: //ORA-01427
40
			return sqlcommons.SubqueryReturnsMoreThanOneRow
41
		default:
42
			return fmt.Errorf("Unhandled Oracle error. Code:[%d] Desc:[%s]", oraError.Code(), oraError.Message())
43
		}
44
	} else {
45
		return err
46
	}
47
}
48