Plugin load errors: Add more well-known errors (#85960)

This commit is contained in:
Andres Martinez Gotor 2024-04-18 13:04:22 +02:00 committed by GitHub
parent 80e63b5206
commit 0f4a47b180
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 36 additions and 6 deletions

View File

@ -43,6 +43,8 @@ export enum PluginErrorCode {
missingSignature = 'signatureMissing',
invalidSignature = 'signatureInvalid',
modifiedSignature = 'signatureModified',
failedBackendStart = 'failedBackendStart',
angular = 'angular',
}
/** Describes error returned from Grafana plugins API call */

View File

@ -79,8 +79,11 @@ func newBackendProcessStarter(processManager process.Manager) *BackendClientStar
// Start will start the backend plugin process.
func (b *BackendClientStarter) Start(ctx context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
if err := b.processManager.Start(ctx, p); err != nil {
b.log.Error("Could not start plugin", "pluginId", p.ID, "error", err)
return nil, err
b.log.Error("Could not start plugin backend", "pluginId", p.ID, "error", err)
return nil, (&plugins.Error{
PluginID: p.ID,
ErrorCode: plugins.ErrorCodeFailedBackendStart,
}).WithMessage(err.Error())
}
return p, nil
}

View File

@ -106,7 +106,10 @@ func (a *AngularDetector) Validate(ctx context.Context, p *plugins.Plugin) error
// Do not initialize plugins if they're using Angular and Angular support is disabled
if p.Angular.Detected && !a.cfg.AngularSupportEnabled {
a.log.Error("Refusing to initialize plugin because it's using Angular, which has been disabled", "pluginId", p.ID)
return errors.New("angular plugins are not supported")
return (&plugins.Error{
PluginID: p.ID,
ErrorCode: plugins.ErrorAngular,
}).WithMessage("angular plugins are not supported")
}
}
p.Angular.HideDeprecation = slices.Contains(a.cfg.HideAngularDeprecation, p.ID)

View File

@ -235,9 +235,11 @@ type AppDTO struct {
}
const (
errorCodeSignatureMissing ErrorCode = "signatureMissing"
errorCodeSignatureModified ErrorCode = "signatureModified"
errorCodeSignatureInvalid ErrorCode = "signatureInvalid"
errorCodeSignatureMissing ErrorCode = "signatureMissing"
errorCodeSignatureModified ErrorCode = "signatureModified"
errorCodeSignatureInvalid ErrorCode = "signatureInvalid"
ErrorCodeFailedBackendStart ErrorCode = "failedBackendStart"
ErrorAngular ErrorCode = "angular"
)
type ErrorCode string
@ -246,9 +248,14 @@ type Error struct {
ErrorCode `json:"errorCode"`
PluginID string `json:"pluginId,omitempty"`
SignatureStatus SignatureStatus `json:"status,omitempty"`
message string `json:"-"`
}
func (e Error) Error() string {
if e.message != "" {
return e.message
}
if e.SignatureStatus != "" {
switch e.SignatureStatus {
case SignatureStatusInvalid:
@ -266,6 +273,10 @@ func (e Error) Error() string {
}
func (e Error) AsErrorCode() ErrorCode {
if e.ErrorCode != "" {
return e.ErrorCode
}
switch e.SignatureStatus {
case SignatureStatusInvalid:
return errorCodeSignatureInvalid
@ -280,6 +291,11 @@ func (e Error) AsErrorCode() ErrorCode {
return ""
}
func (e *Error) WithMessage(m string) *Error {
e.message = m
return e
}
// Access-Control related definitions
// RoleRegistration stores a role and its assignments to basic roles

View File

@ -89,6 +89,7 @@ func newRegisterPluginRoles(registry plugins.RoleRegistry) *RegisterPluginRoles
func (r *RegisterPluginRoles) Register(ctx context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
if err := r.roleRegistry.DeclarePluginRoles(ctx, p.ID, p.Name, p.Roles); err != nil {
r.log.Warn("Declare plugin roles failed.", "pluginId", p.ID, "error", err)
return nil, err
}
return p, nil
}

View File

@ -66,6 +66,11 @@ function renderDescriptionFromError(error?: PluginErrorCode): ReactElement {
version of this plugin.
</p>
);
case PluginErrorCode.failedBackendStart:
return <p>This plugin failed to start. Server logs can provide more information.</p>;
case PluginErrorCode.angular:
// Error message already rendered by AngularDeprecationPluginNotice
return <></>;
default:
return (
<p>