|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package commands |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + |
| 21 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 22 | + "google.golang.org/grpc/codes" |
| 23 | + "google.golang.org/grpc/status" |
| 24 | +) |
| 25 | + |
| 26 | +func composeErrorMsg(msg string, cause error) string { |
| 27 | + if cause == nil { |
| 28 | + return msg |
| 29 | + } |
| 30 | + return fmt.Sprintf("%v: %v", msg, cause) |
| 31 | +} |
| 32 | + |
| 33 | +// CommandError is an error that may be converted into a gRPC status. |
| 34 | +type CommandError interface { |
| 35 | + ToRPCStatus() *status.Status |
| 36 | +} |
| 37 | + |
| 38 | +// InvalidInstanceError is returned if the instance used in the command is not valid. |
| 39 | +type InvalidInstanceError struct{} |
| 40 | + |
| 41 | +func (e *InvalidInstanceError) Error() string { |
| 42 | + return tr("Invalid instance") |
| 43 | +} |
| 44 | + |
| 45 | +func (e *InvalidInstanceError) ToRPCStatus() *status.Status { |
| 46 | + return status.New(codes.InvalidArgument, e.Error()) |
| 47 | +} |
| 48 | + |
| 49 | +// InvalidFQBNError is returned when the FQBN has syntax errors |
| 50 | +type InvalidFQBNError struct { |
| 51 | + Cause error |
| 52 | +} |
| 53 | + |
| 54 | +func (e *InvalidFQBNError) Error() string { |
| 55 | + return composeErrorMsg(tr("Invalid FQBN"), e.Cause) |
| 56 | +} |
| 57 | + |
| 58 | +func (e *InvalidFQBNError) ToRPCStatus() *status.Status { |
| 59 | + return status.New(codes.InvalidArgument, e.Error()) |
| 60 | +} |
| 61 | + |
| 62 | +func (e *InvalidFQBNError) Unwrap() error { |
| 63 | + return e.Cause |
| 64 | +} |
| 65 | + |
| 66 | +// MissingFQBNError is returned when the FQBN is mandatory and not specified |
| 67 | +type MissingFQBNError struct{} |
| 68 | + |
| 69 | +func (e *MissingFQBNError) Error() string { |
| 70 | + return tr("Missing FQBN (Fully Qualified Board Name)") |
| 71 | +} |
| 72 | + |
| 73 | +func (e *MissingFQBNError) ToRPCStatus() *status.Status { |
| 74 | + return status.New(codes.InvalidArgument, e.Error()) |
| 75 | +} |
| 76 | + |
| 77 | +// UnknownFQBNError is returned when the FQBN is not found |
| 78 | +type UnknownFQBNError struct { |
| 79 | + Cause error |
| 80 | +} |
| 81 | + |
| 82 | +func (e *UnknownFQBNError) Error() string { |
| 83 | + return composeErrorMsg(tr("Unknown FQBN"), e.Cause) |
| 84 | +} |
| 85 | + |
| 86 | +func (e *UnknownFQBNError) Unwrap() error { |
| 87 | + return e.Cause |
| 88 | +} |
| 89 | + |
| 90 | +func (e *UnknownFQBNError) ToRPCStatus() *status.Status { |
| 91 | + return status.New(codes.NotFound, e.Error()) |
| 92 | +} |
| 93 | + |
| 94 | +// MissingPortProtocolError is returned when the port protocol is mandatory and not specified |
| 95 | +type MissingPortProtocolError struct{} |
| 96 | + |
| 97 | +func (e *MissingPortProtocolError) Error() string { |
| 98 | + return tr("Missing port protocol") |
| 99 | +} |
| 100 | + |
| 101 | +func (e *MissingPortProtocolError) ToRPCStatus() *status.Status { |
| 102 | + return status.New(codes.InvalidArgument, e.Error()) |
| 103 | +} |
| 104 | + |
| 105 | +// MissingProgrammerError is returned when the programmer is mandatory and not specified |
| 106 | +type MissingProgrammerError struct{} |
| 107 | + |
| 108 | +func (e *MissingProgrammerError) Error() string { |
| 109 | + return tr("Missing programmer") |
| 110 | +} |
| 111 | + |
| 112 | +func (e *MissingProgrammerError) ToRPCStatus() *status.Status { |
| 113 | + return status.New(codes.InvalidArgument, e.Error()) |
| 114 | +} |
| 115 | + |
| 116 | +// ProgreammerRequiredForUploadError is returned then the upload can be done only using a programmer |
| 117 | +type ProgreammerRequiredForUploadError struct{} |
| 118 | + |
| 119 | +func (e *ProgreammerRequiredForUploadError) Error() string { |
| 120 | + return tr("A programmer is required to upload") |
| 121 | +} |
| 122 | + |
| 123 | +func (e *ProgreammerRequiredForUploadError) ToRPCStatus() *status.Status { |
| 124 | + st, _ := status. |
| 125 | + New(codes.InvalidArgument, e.Error()). |
| 126 | + WithDetails(&rpc.ProgrammerIsRequiredForUploadError{}) |
| 127 | + return st |
| 128 | +} |
| 129 | + |
| 130 | +// UnknownProgrammerError is returned when the programmer is not found |
| 131 | +type UnknownProgrammerError struct { |
| 132 | + Cause error |
| 133 | +} |
| 134 | + |
| 135 | +func (e *UnknownProgrammerError) Error() string { |
| 136 | + return composeErrorMsg(tr("Unknown programmer"), e.Cause) |
| 137 | +} |
| 138 | + |
| 139 | +func (e *UnknownProgrammerError) Unwrap() error { |
| 140 | + return e.Cause |
| 141 | +} |
| 142 | + |
| 143 | +func (e *UnknownProgrammerError) ToRPCStatus() *status.Status { |
| 144 | + return status.New(codes.NotFound, e.Error()) |
| 145 | +} |
| 146 | + |
| 147 | +// InvalidPlatformPropertyError is returned when a property in the platform is not valid |
| 148 | +type InvalidPlatformPropertyError struct { |
| 149 | + Property string |
| 150 | + Value string |
| 151 | +} |
| 152 | + |
| 153 | +func (e *InvalidPlatformPropertyError) Error() string { |
| 154 | + return tr("Invalid '%[1]s' property: %[2]s", e.Property, e.Value) |
| 155 | +} |
| 156 | + |
| 157 | +func (e *InvalidPlatformPropertyError) ToRPCStatus() *status.Status { |
| 158 | + return status.New(codes.FailedPrecondition, e.Error()) |
| 159 | +} |
| 160 | + |
| 161 | +// MissingPlatformPropertyError is returned when a property in the platform is not found |
| 162 | +type MissingPlatformPropertyError struct { |
| 163 | + Property string |
| 164 | +} |
| 165 | + |
| 166 | +func (e *MissingPlatformPropertyError) Error() string { |
| 167 | + return tr("Property '%s' is undefined", e.Property) |
| 168 | +} |
| 169 | + |
| 170 | +func (e *MissingPlatformPropertyError) ToRPCStatus() *status.Status { |
| 171 | + return status.New(codes.FailedPrecondition, e.Error()) |
| 172 | +} |
| 173 | + |
| 174 | +// SketchNotFoundError is returned when the sketch is not found |
| 175 | +type SketchNotFoundError struct { |
| 176 | + Cause error |
| 177 | +} |
| 178 | + |
| 179 | +func (e *SketchNotFoundError) Error() string { |
| 180 | + return composeErrorMsg(tr("Sketch not found"), e.Cause) |
| 181 | +} |
| 182 | + |
| 183 | +func (e *SketchNotFoundError) Unwrap() error { |
| 184 | + return e.Cause |
| 185 | +} |
| 186 | + |
| 187 | +func (e *SketchNotFoundError) ToRPCStatus() *status.Status { |
| 188 | + return status.New(codes.NotFound, e.Error()) |
| 189 | +} |
| 190 | + |
| 191 | +// FailedUploadError is returned when the upload fails |
| 192 | +type FailedUploadError struct { |
| 193 | + Message string |
| 194 | + Cause error |
| 195 | +} |
| 196 | + |
| 197 | +func (e *FailedUploadError) Error() string { |
| 198 | + return composeErrorMsg(e.Message, e.Cause) |
| 199 | +} |
| 200 | + |
| 201 | +func (e *FailedUploadError) Unwrap() error { |
| 202 | + return e.Cause |
| 203 | +} |
| 204 | + |
| 205 | +func (e *FailedUploadError) ToRPCStatus() *status.Status { |
| 206 | + return status.New(codes.Internal, e.Error()) |
| 207 | +} |
0 commit comments