2016-03-26 02:38:00 +03:00
|
|
|
// +build !windows,!solaris
|
2015-05-28 22:21:32 +03:00
|
|
|
|
2015-01-08 01:43:04 +03:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-01-08 03:22:42 +03:00
|
|
|
"bufio"
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 18:53:35 +03:00
|
|
|
"fmt"
|
2015-01-08 03:22:42 +03:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-01-08 01:43:04 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-03-27 01:22:04 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
2015-01-20 02:29:42 +03:00
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
2016-04-08 05:09:07 +03:00
|
|
|
sysinfo "github.com/docker/docker/pkg/system"
|
2016-03-18 21:50:19 +03:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-07-17 02:00:55 +03:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2015-01-08 01:43:04 +03:00
|
|
|
)
|
|
|
|
|
2015-11-03 22:06:16 +03:00
|
|
|
type statsSupervisor interface {
|
|
|
|
// GetContainerStats collects all the stats related to a container
|
2016-03-18 21:50:19 +03:00
|
|
|
GetContainerStats(container *container.Container) (*types.StatsJSON, error)
|
2015-11-03 22:06:16 +03:00
|
|
|
}
|
|
|
|
|
2015-01-08 05:02:08 +03:00
|
|
|
// newStatsCollector returns a new statsCollector that collections
|
|
|
|
// network and cgroup stats for a registered container at the specified
|
|
|
|
// interval. The collector allows non-running containers to be added
|
|
|
|
// and will start processing stats when they are started.
|
2015-11-03 22:06:16 +03:00
|
|
|
func (daemon *Daemon) newStatsCollector(interval time.Duration) *statsCollector {
|
2015-01-08 01:43:04 +03:00
|
|
|
s := &statsCollector{
|
2015-07-31 00:01:53 +03:00
|
|
|
interval: interval,
|
2015-11-03 22:06:16 +03:00
|
|
|
supervisor: daemon,
|
2015-11-12 22:55:17 +03:00
|
|
|
publishers: make(map[*container.Container]*pubsub.Publisher),
|
2015-07-31 00:01:53 +03:00
|
|
|
clockTicksPerSecond: uint64(system.GetClockTicks()),
|
|
|
|
bufReader: bufio.NewReaderSize(nil, 128),
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
2016-04-08 05:09:07 +03:00
|
|
|
meminfo, err := sysinfo.ReadMemInfo()
|
|
|
|
if err == nil && meminfo.MemTotal > 0 {
|
|
|
|
s.machineMemory = uint64(meminfo.MemTotal)
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:29:42 +03:00
|
|
|
go s.run()
|
2015-01-08 01:43:04 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// statsCollector manages and provides container resource stats
|
|
|
|
type statsCollector struct {
|
2015-07-31 00:01:53 +03:00
|
|
|
m sync.Mutex
|
2015-11-03 22:06:16 +03:00
|
|
|
supervisor statsSupervisor
|
2015-07-31 00:01:53 +03:00
|
|
|
interval time.Duration
|
|
|
|
clockTicksPerSecond uint64
|
2015-11-12 22:55:17 +03:00
|
|
|
publishers map[*container.Container]*pubsub.Publisher
|
2015-07-31 00:01:53 +03:00
|
|
|
bufReader *bufio.Reader
|
2016-04-08 05:09:07 +03:00
|
|
|
machineMemory uint64
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
|
|
|
|
2015-01-08 05:02:08 +03:00
|
|
|
// collect registers the container with the collector and adds it to
|
|
|
|
// the event loop for collection on the specified interval returning
|
|
|
|
// a channel for the subscriber to receive on.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (s *statsCollector) collect(c *container.Container) chan interface{} {
|
2015-01-08 01:43:04 +03:00
|
|
|
s.m.Lock()
|
2015-01-08 05:02:08 +03:00
|
|
|
defer s.m.Unlock()
|
2015-01-20 02:29:42 +03:00
|
|
|
publisher, exists := s.publishers[c]
|
|
|
|
if !exists {
|
|
|
|
publisher = pubsub.NewPublisher(100*time.Millisecond, 1024)
|
|
|
|
s.publishers[c] = publisher
|
2015-01-08 05:02:08 +03:00
|
|
|
}
|
2015-01-20 02:29:42 +03:00
|
|
|
return publisher.Subscribe()
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
|
|
|
|
2015-01-08 05:02:08 +03:00
|
|
|
// stopCollection closes the channels for all subscribers and removes
|
|
|
|
// the container from metrics collection.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (s *statsCollector) stopCollection(c *container.Container) {
|
2015-01-08 01:43:04 +03:00
|
|
|
s.m.Lock()
|
2015-01-20 02:29:42 +03:00
|
|
|
if publisher, exists := s.publishers[c]; exists {
|
|
|
|
publisher.Close()
|
|
|
|
delete(s.publishers, c)
|
2015-01-08 05:02:08 +03:00
|
|
|
}
|
2015-01-20 02:29:42 +03:00
|
|
|
s.m.Unlock()
|
2015-01-08 05:02:08 +03:00
|
|
|
}
|
|
|
|
|
2015-01-20 02:29:42 +03:00
|
|
|
// unsubscribe removes a specific subscriber from receiving updates for a container's stats.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (s *statsCollector) unsubscribe(c *container.Container, ch chan interface{}) {
|
2015-01-08 05:02:08 +03:00
|
|
|
s.m.Lock()
|
2015-01-20 02:29:42 +03:00
|
|
|
publisher := s.publishers[c]
|
|
|
|
if publisher != nil {
|
|
|
|
publisher.Evict(ch)
|
2015-01-20 22:37:50 +03:00
|
|
|
if publisher.Len() == 0 {
|
|
|
|
delete(s.publishers, c)
|
|
|
|
}
|
2015-01-08 05:02:08 +03:00
|
|
|
}
|
2015-01-08 01:43:04 +03:00
|
|
|
s.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:29:42 +03:00
|
|
|
func (s *statsCollector) run() {
|
2015-04-24 00:39:05 +03:00
|
|
|
type publishersPair struct {
|
2015-11-12 22:55:17 +03:00
|
|
|
container *container.Container
|
2015-04-24 00:39:05 +03:00
|
|
|
publisher *pubsub.Publisher
|
|
|
|
}
|
|
|
|
// we cannot determine the capacity here.
|
|
|
|
// it will grow enough in first iteration
|
|
|
|
var pairs []publishersPair
|
|
|
|
|
2015-04-20 11:08:01 +03:00
|
|
|
for range time.Tick(s.interval) {
|
2015-04-24 00:39:05 +03:00
|
|
|
// it does not make sense in the first iteration,
|
|
|
|
// but saves allocations in further iterations
|
|
|
|
pairs = pairs[:0]
|
|
|
|
|
|
|
|
s.m.Lock()
|
2015-01-20 02:29:42 +03:00
|
|
|
for container, publisher := range s.publishers {
|
2015-04-24 00:39:05 +03:00
|
|
|
// copy pointers here to release the lock ASAP
|
|
|
|
pairs = append(pairs, publishersPair{container, publisher})
|
|
|
|
}
|
|
|
|
s.m.Unlock()
|
2015-09-08 21:57:22 +03:00
|
|
|
if len(pairs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
systemUsage, err := s.getSystemCPUUsage()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("collecting system cpu usage: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-24 00:39:05 +03:00
|
|
|
|
|
|
|
for _, pair := range pairs {
|
2015-11-03 22:06:16 +03:00
|
|
|
stats, err := s.supervisor.GetContainerStats(pair.container)
|
2015-01-20 02:29:42 +03:00
|
|
|
if err != nil {
|
2016-03-23 00:01:51 +03:00
|
|
|
if _, ok := err.(errNotRunning); !ok {
|
2015-04-24 00:39:05 +03:00
|
|
|
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
2015-01-20 02:29:42 +03:00
|
|
|
continue
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
2016-03-18 21:50:19 +03:00
|
|
|
// FIXME: move to containerd
|
|
|
|
stats.CPUStats.SystemUsage = systemUsage
|
2015-11-03 05:06:44 +03:00
|
|
|
|
2016-04-18 21:12:07 +03:00
|
|
|
pair.publisher.Publish(*stats)
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
2015-01-20 02:29:42 +03:00
|
|
|
}
|
2015-01-08 01:43:04 +03:00
|
|
|
}
|
2015-01-08 03:22:42 +03:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
const nanoSecondsPerSecond = 1e9
|
2015-01-20 01:07:21 +03:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// getSystemCPUUsage returns the host system's cpu usage in
|
|
|
|
// nanoseconds. An error is returned if the format of the underlying
|
|
|
|
// file does not match.
|
|
|
|
//
|
|
|
|
// Uses /proc/stat defined by POSIX. Looks for the cpu
|
|
|
|
// statistics line and then sums up the first seven fields
|
|
|
|
// provided. See `man 5 proc` for details on specific field
|
|
|
|
// information.
|
|
|
|
func (s *statsCollector) getSystemCPUUsage() (uint64, error) {
|
2015-05-15 12:22:50 +03:00
|
|
|
var line string
|
2015-01-08 03:22:42 +03:00
|
|
|
f, err := os.Open("/proc/stat")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2015-05-15 12:22:50 +03:00
|
|
|
defer func() {
|
|
|
|
s.bufReader.Reset(nil)
|
|
|
|
f.Close()
|
|
|
|
}()
|
|
|
|
s.bufReader.Reset(f)
|
|
|
|
err = nil
|
|
|
|
for err == nil {
|
|
|
|
line, err = s.bufReader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parts := strings.Fields(line)
|
2015-01-08 03:22:42 +03:00
|
|
|
switch parts[0] {
|
|
|
|
case "cpu":
|
|
|
|
if len(parts) < 8 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 18:53:35 +03:00
|
|
|
return 0, fmt.Errorf("invalid number of cpu fields")
|
2015-01-08 03:22:42 +03:00
|
|
|
}
|
2015-07-31 00:01:53 +03:00
|
|
|
var totalClockTicks uint64
|
2015-01-08 03:22:42 +03:00
|
|
|
for _, i := range parts[1:8] {
|
|
|
|
v, err := strconv.ParseUint(i, 10, 64)
|
|
|
|
if err != nil {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 18:53:35 +03:00
|
|
|
return 0, fmt.Errorf("Unable to convert value %s to int: %s", i, err)
|
2015-01-08 03:22:42 +03:00
|
|
|
}
|
2015-07-31 00:01:53 +03:00
|
|
|
totalClockTicks += v
|
2015-01-08 03:22:42 +03:00
|
|
|
}
|
2015-07-31 00:01:53 +03:00
|
|
|
return (totalClockTicks * nanoSecondsPerSecond) /
|
|
|
|
s.clockTicksPerSecond, nil
|
2015-01-08 03:22:42 +03:00
|
|
|
}
|
|
|
|
}
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 18:53:35 +03:00
|
|
|
return 0, fmt.Errorf("invalid stat format. Error trying to parse the '/proc/stat' file")
|
2015-01-08 03:22:42 +03:00
|
|
|
}
|