github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/notice.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/settings"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
    19  	"github.com/cockroachdb/cockroach/pkg/util/log"
    20  )
    21  
    22  // NoticesEnabled is the cluster setting that allows users
    23  // to enable notices.
    24  var NoticesEnabled = settings.RegisterPublicBoolSetting(
    25  	"sql.notices.enabled",
    26  	"enable notices in the server/client protocol being sent",
    27  	true,
    28  )
    29  
    30  // noticeSender is a subset of RestrictedCommandResult which allows
    31  // sending notices.
    32  type noticeSender interface {
    33  	AppendNotice(error)
    34  }
    35  
    36  // SendClientNotice implements the tree.ClientNoticeSender interface.
    37  func (p *planner) SendClientNotice(ctx context.Context, err error) {
    38  	if log.V(2) {
    39  		log.Infof(ctx, "out-of-band notice: %+v", err)
    40  	}
    41  	noticeSeverity, ok := pgnotice.ParseDisplaySeverity(pgerror.GetSeverity(err))
    42  	if !ok {
    43  		noticeSeverity = pgnotice.DisplaySeverityNotice
    44  	}
    45  	if p.noticeSender == nil ||
    46  		noticeSeverity > p.SessionData().NoticeDisplaySeverity ||
    47  		!NoticesEnabled.Get(&p.execCfg.Settings.SV) {
    48  		// Notice cannot flow to the client - because of one of these conditions:
    49  		// * there is no client
    50  		// * the session's NoticeDisplaySeverity is higher than the severity of the notice.
    51  		// * the notice protocol was disabled
    52  		return
    53  	}
    54  	p.noticeSender.AppendNotice(err)
    55  }