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,128 @@ 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
+ } as const ;
81
+
82
+ export const ConnectionFailedError = createMonitorError (
83
+ MonitorErrorCodes . ConnectionFailed
84
+ ) ;
85
+ export const NotConnectedError = createMonitorError (
86
+ MonitorErrorCodes . NotConnected
87
+ ) ;
88
+ export const AlreadyConnectedError = createMonitorError (
89
+ MonitorErrorCodes . AlreadyConnected
90
+ ) ;
91
+ export const MissingConfigurationError = createMonitorError (
92
+ MonitorErrorCodes . MissingConfiguration
93
+ ) ;
94
+
95
+ export function createConnectionFailedError (
96
+ port : Port ,
97
+ details ?: string
98
+ ) : ApplicationError < number , PortDescriptor > {
99
+ const { protocol, protocolLabel, address, addressLabel } = port ;
100
+ return ConnectionFailedError (
101
+ connectionFailedErrorLabel ( addressLabel , protocolLabel , details ) ,
102
+ { protocol, address }
103
+ ) ;
79
104
}
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
- } ;
105
+
106
+ export function createNotConnectedError (
107
+ port : Port
108
+ ) : ApplicationError < number , PortDescriptor > {
109
+ const { protocol, protocolLabel, address, addressLabel } = port ;
110
+ return NotConnectedError (
111
+ notConnectedErrorLabel ( addressLabel , protocolLabel ) ,
112
+ { protocol, address }
113
+ ) ;
114
+ }
115
+
116
+ export function createAlreadyConnectedError (
117
+ port : Port
118
+ ) : ApplicationError < number , PortDescriptor > {
119
+ const { protocol, protocolLabel, address, addressLabel } = port ;
120
+ return AlreadyConnectedError (
121
+ alreadyConnectedErrorLabel ( addressLabel , protocolLabel ) ,
122
+ { protocol, address }
123
+ ) ;
124
+ }
125
+
126
+ export function createMissingConfigurationError (
127
+ port : Port
128
+ ) : ApplicationError < number , PortDescriptor > {
129
+ const { protocol, protocolLabel, address, addressLabel } = port ;
130
+ return MissingConfigurationError (
131
+ missingConfigurationErrorLabel ( addressLabel , protocolLabel ) ,
132
+ { protocol, address }
133
+ ) ;
134
+ }
135
+
136
+ /**
137
+ * Bare minimum representation of a port. Supports neither UI labels nor properties.
138
+ */
139
+ interface PortDescriptor {
140
+ readonly protocol : string ;
141
+ readonly address : string ;
142
+ }
143
+
144
+ function createMonitorError (
145
+ code : number
146
+ ) : ApplicationError . Constructor < number , PortDescriptor > {
147
+ return ApplicationError . declare (
148
+ code ,
149
+ ( message : string , data : PortDescriptor ) => ( { data, message } )
150
+ ) ;
151
+ }
152
+
153
+ export function connectionFailedErrorLabel (
154
+ addressLabel : string ,
155
+ protocolLabel : string ,
156
+ details ?: string
157
+ ) : string {
158
+ const formattedDetails = details ? `: ${ details } .` : '.' ;
159
+ return nls . localize (
160
+ 'arduino/monitor/connectionFailedError' ,
161
+ 'Could not connect to {0} {1} port{2}' ,
162
+ addressLabel ,
163
+ protocolLabel ,
164
+ formattedDetails
165
+ ) ;
166
+ }
167
+ export function notConnectedErrorLabel (
168
+ addressLabel : string ,
169
+ protocolLabel : string
170
+ ) : string {
171
+ return nls . localize (
172
+ 'arduino/monitor/notConnectedError' ,
173
+ 'Not connected to {0} {1} port.' ,
174
+ addressLabel ,
175
+ protocolLabel
176
+ ) ;
177
+ }
178
+ export function alreadyConnectedErrorLabel (
179
+ addressLabel : string ,
180
+ protocolLabel : string
181
+ ) : string {
182
+ return nls . localize (
183
+ 'arduino/monitor/alreadyConnectedError' ,
184
+ 'Could not connect to {0} {1} port. Already connected.' ,
185
+ addressLabel ,
186
+ protocolLabel
187
+ ) ;
188
+ }
189
+ export function missingConfigurationErrorLabel (
190
+ addressLabel : string ,
191
+ protocolLabel : string
192
+ ) : string {
193
+ return nls . localize (
194
+ 'arduino/monitor/missingConfigurationError' ,
195
+ 'Could not connect to {0} {1} port. The monitor configuration is missing.' ,
196
+ addressLabel ,
197
+ protocolLabel
198
+ ) ;
95
199
}
0 commit comments