1
- import { Event , JsonRpcServer } from '@theia/core' ;
1
+ import { ApplicationError , Event , JsonRpcServer , nls } from '@theia/core' ;
2
2
import {
3
3
PluggableMonitorSettings ,
4
4
MonitorSettings ,
@@ -46,7 +46,7 @@ export interface PluggableMonitorSetting {
46
46
readonly id : string ;
47
47
// A human-readable label of the setting (to be displayed on the GUI)
48
48
readonly label : string ;
49
- // The setting type (at the moment only "enum" is avaiable )
49
+ // The setting type (at the moment only "enum" is available )
50
50
readonly type : string ;
51
51
// The values allowed on "enum" types
52
52
readonly values : string [ ] ;
@@ -72,24 +72,158 @@ export namespace Monitor {
72
72
} ;
73
73
}
74
74
75
- export interface Status { }
76
- export type OK = Status ;
77
- export interface ErrorStatus extends Status {
78
- readonly message : string ;
75
+ export const MonitorErrorCodes = {
76
+ ConnectionFailed : 6001 ,
77
+ NotConnected : 6002 ,
78
+ AlreadyConnected : 6003 ,
79
+ MissingConfiguration : 6004 ,
80
+ UploadInProgress : 6005 ,
81
+ } as const ;
82
+
83
+ export const ConnectionFailedError = createMonitorError (
84
+ MonitorErrorCodes . ConnectionFailed
85
+ ) ;
86
+ export const NotConnectedError = createMonitorError (
87
+ MonitorErrorCodes . NotConnected
88
+ ) ;
89
+ export const AlreadyConnectedError = createMonitorError (
90
+ MonitorErrorCodes . AlreadyConnected
91
+ ) ;
92
+ export const MissingConfigurationError = createMonitorError (
93
+ MonitorErrorCodes . MissingConfiguration
94
+ ) ;
95
+ export const UploadInProgressError = createMonitorError (
96
+ MonitorErrorCodes . UploadInProgress
97
+ ) ;
98
+
99
+ export function createConnectionFailedError (
100
+ port : Port ,
101
+ details ?: string
102
+ ) : ApplicationError < number , PortDescriptor > {
103
+ const { protocol, protocolLabel, address, addressLabel } = port ;
104
+ return ConnectionFailedError (
105
+ connectionFailedErrorLabel ( addressLabel , protocolLabel , details ) ,
106
+ { protocol, address }
107
+ ) ;
79
108
}
80
- export namespace Status {
81
- export function isOK ( status : Status & { message ?: string } ) : status is OK {
82
- return ! ! status && typeof status . message !== 'string' ;
83
- }
84
- export const OK : OK = { } ;
85
- export const NOT_CONNECTED : ErrorStatus = { message : 'Not connected.' } ;
86
- export const ALREADY_CONNECTED : ErrorStatus = {
87
- message : 'Already connected.' ,
88
- } ;
89
- export const CONFIG_MISSING : ErrorStatus = {
90
- message : 'Serial Config missing.' ,
91
- } ;
92
- export const UPLOAD_IN_PROGRESS : ErrorStatus = {
93
- message : 'Upload in progress.' ,
94
- } ;
109
+
110
+ export function createNotConnectedError (
111
+ port : Port
112
+ ) : ApplicationError < number , PortDescriptor > {
113
+ const { protocol, protocolLabel, address, addressLabel } = port ;
114
+ return NotConnectedError (
115
+ notConnectedErrorLabel ( addressLabel , protocolLabel ) ,
116
+ { protocol, address }
117
+ ) ;
118
+ }
119
+
120
+ export function createAlreadyConnectedError (
121
+ port : Port
122
+ ) : ApplicationError < number , PortDescriptor > {
123
+ const { protocol, protocolLabel, address, addressLabel } = port ;
124
+ return AlreadyConnectedError (
125
+ alreadyConnectedErrorLabel ( addressLabel , protocolLabel ) ,
126
+ { protocol, address }
127
+ ) ;
128
+ }
129
+
130
+ export function createMissingConfigurationError (
131
+ port : Port
132
+ ) : ApplicationError < number , PortDescriptor > {
133
+ const { protocol, protocolLabel, address, addressLabel } = port ;
134
+ return MissingConfigurationError (
135
+ missingConfigurationErrorLabel ( addressLabel , protocolLabel ) ,
136
+ { protocol, address }
137
+ ) ;
138
+ }
139
+
140
+ export function createUploadInProgressError (
141
+ port : Port
142
+ ) : ApplicationError < number , PortDescriptor > {
143
+ const { protocol, protocolLabel, address, addressLabel } = port ;
144
+ return UploadInProgressError (
145
+ uploadInProgressErrorLabel ( addressLabel , protocolLabel ) ,
146
+ { protocol, address }
147
+ ) ;
148
+ }
149
+
150
+ /**
151
+ * Bare minimum representation of a port. Supports neither UI labels nor properties.
152
+ */
153
+ export interface PortDescriptor {
154
+ readonly protocol : string ;
155
+ readonly address : string ;
156
+ }
157
+
158
+ export function createPortDescriptor ( port : Port ) : PortDescriptor {
159
+ const { protocol, address } = port ;
160
+ return { protocol, address } ;
161
+ }
162
+
163
+ function createMonitorError (
164
+ code : number
165
+ ) : ApplicationError . Constructor < number , PortDescriptor > {
166
+ return ApplicationError . declare (
167
+ code ,
168
+ ( message : string , data : PortDescriptor ) => ( { data, message } )
169
+ ) ;
170
+ }
171
+
172
+ export function connectionFailedErrorLabel (
173
+ addressLabel : string ,
174
+ protocolLabel : string ,
175
+ details ?: string
176
+ ) : string {
177
+ const formattedDetails = details ? `: ${ details } .` : '.' ;
178
+ return nls . localize (
179
+ 'arduino/monitor/connectionFailedError' ,
180
+ 'Could not connect to {0} {1} port{2}' ,
181
+ addressLabel ,
182
+ protocolLabel ,
183
+ formattedDetails
184
+ ) ;
185
+ }
186
+ export function notConnectedErrorLabel (
187
+ addressLabel : string ,
188
+ protocolLabel : string
189
+ ) : string {
190
+ return nls . localize (
191
+ 'arduino/monitor/notConnectedError' ,
192
+ 'Not connected to {0} {1} port.' ,
193
+ addressLabel ,
194
+ protocolLabel
195
+ ) ;
196
+ }
197
+ export function missingConfigurationErrorLabel (
198
+ addressLabel : string ,
199
+ protocolLabel : string
200
+ ) : string {
201
+ return nls . localize (
202
+ 'arduino/monitor/missingConfigurationError' ,
203
+ 'Could not connect to {0} {1} port. The monitor configuration is missing.' ,
204
+ addressLabel ,
205
+ protocolLabel
206
+ ) ;
207
+ }
208
+ export function uploadInProgressErrorLabel (
209
+ addressLabel : string ,
210
+ protocolLabel : string
211
+ ) : string {
212
+ return nls . localize (
213
+ 'arduino/monitor/uploadInProgressError' ,
214
+ 'Could not connect to {0} {1} port. An upload is in progress.' ,
215
+ addressLabel ,
216
+ protocolLabel
217
+ ) ;
218
+ }
219
+ export function alreadyConnectedErrorLabel (
220
+ addressLabel : string ,
221
+ protocolLabel : string
222
+ ) : string {
223
+ return nls . localize (
224
+ 'arduino/monitor/alreadyConnectedError' ,
225
+ 'Could not connect to {0} {1} port. Already connected.' ,
226
+ addressLabel ,
227
+ protocolLabel
228
+ ) ;
95
229
}
0 commit comments