web: predeclare and reuse errors (#5180)

Predeclare and reuse errors to reduce duplicate code

Signed-off-by: zhulongcheng <zhulongcheng.me@gmail.com>
This commit is contained in:
zhulongcheng 2019-02-04 20:06:26 +08:00 committed by Goutham Veeramachaneni
parent a75f8a8e05
commit fd964426a7
1 changed files with 11 additions and 6 deletions

View File

@ -111,6 +111,11 @@ var (
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999)
)
var (
errAdminDisabled = status.Error(codes.Unavailable, "Admin APIs are disabled")
errTSDBNotReady = status.Error(codes.Unavailable, "TSDB not ready")
)
// AdminDisabled implements the administration interface that informs
// that the API endpoints are disabled.
type AdminDisabled struct {
@ -118,17 +123,17 @@ type AdminDisabled struct {
// TSDBSnapshot implements pb.AdminServer.
func (s *AdminDisabled) TSDBSnapshot(_ old_ctx.Context, _ *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
return nil, errAdminDisabled
}
// TSDBCleanTombstones implements pb.AdminServer.
func (s *AdminDisabled) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
return nil, errAdminDisabled
}
// DeleteSeries implements pb.AdminServer.
func (s *AdminDisabled) DeleteSeries(_ old_ctx.Context, r *pb.SeriesDeleteRequest) (*pb.SeriesDeleteResponse, error) {
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
return nil, errAdminDisabled
}
// Admin provides an administration interface to Prometheus.
@ -147,7 +152,7 @@ func NewAdmin(db func() *tsdb.DB) *Admin {
func (s *Admin) TSDBSnapshot(_ old_ctx.Context, req *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
db := s.db()
if db == nil {
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
return nil, errTSDBNotReady
}
var (
snapdir = filepath.Join(db.Dir(), "snapshots")
@ -169,7 +174,7 @@ func (s *Admin) TSDBSnapshot(_ old_ctx.Context, req *pb.TSDBSnapshotRequest) (*p
func (s *Admin) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
db := s.db()
if db == nil {
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
return nil, errTSDBNotReady
}
if err := db.CleanTombstones(); err != nil {
@ -215,7 +220,7 @@ func (s *Admin) DeleteSeries(_ old_ctx.Context, r *pb.SeriesDeleteRequest) (*pb.
}
db := s.db()
if db == nil {
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
return nil, errTSDBNotReady
}
if err := db.Delete(timestamp.FromTime(mint), timestamp.FromTime(maxt), matchers...); err != nil {
return nil, status.Error(codes.Internal, err.Error())