Source

go-gsl / gsl / errno.go

Full commit
// Copyright (C) 2010  The Go-GSL Authors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package gsl

/*
#cgo LDFLAGS: -lgsl -lgslcblas
#include <gsl/gsl_errno.h>
*/
import "C"

var (
	EDOM   = C.GSL_EDOM
	ERANGE = C.GSL_ERANGE
	ENOMEM = C.GSL_ENOMEM
	EINVAL = C.GSL_EINVAL
)

func init() {
	// Disable the default error handler, which calls abort.
	C.gsl_set_error_handler_off()
}

type Error int

func (e Error) Error() string {
	return C.GoString(C.gsl_strerror(C.int(e)))
}

func NewError(code int) Error {
	return Error(code)
}

func errnoToError(code C.int) error {
	n := int(code)
	if n == 0 {
		return nil
	}
	return NewError(n)
}