| Conditions | 11 |
| Total Lines | 26 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like adapter.*postgresAdapter.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 |
||
| 35 | func (s *postgresAdapter) ErrorHandler(err error) error { |
||
| 36 | if err == nil { |
||
| 37 | return nil |
||
| 38 | } |
||
| 39 | |||
| 40 | if pqError, ok := err.(*pq.Error); ok { |
||
| 41 | switch pqError.Code { |
||
| 42 | case "23505": |
||
| 43 | return sqlcommons.UniqueConstraintViolation |
||
| 44 | case "23503": |
||
| 45 | return sqlcommons.IntegrityConstraintViolation |
||
| 46 | case "22001": |
||
| 47 | return sqlcommons.ValueTooLargeForColumn |
||
| 48 | case "22003": |
||
| 49 | return sqlcommons.ValueLargerThanPrecision |
||
| 50 | case "23502": |
||
| 51 | return sqlcommons.CannotSetNullColumn |
||
| 52 | case "22P02": |
||
| 53 | return sqlcommons.InvalidNumericValue |
||
| 54 | case "21000": |
||
| 55 | return sqlcommons.SubqueryReturnsMoreThanOneRow |
||
| 56 | default: |
||
| 57 | return fmt.Errorf("Unhandled PostgreSQL error. Code:[%s] Desc:[%s]", pqError.Code, pqError.Message) |
||
| 58 | } |
||
| 59 | } else { |
||
| 60 | return err |
||
| 61 | } |
||
| 63 |