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,98 @@ 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
+ const formattedDetails = details ? `: ${ details } .` : '.' ;
101
+ return ConnectionFailedError (
102
+ nls . localize (
103
+ 'arduino/monitor/connectionFailedError' ,
104
+ 'Could not connect to {0} {1} port{2}' ,
105
+ addressLabel ,
106
+ protocolLabel ,
107
+ formattedDetails
108
+ ) ,
109
+ { protocol, address }
110
+ ) ;
79
111
}
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
- } ;
112
+ export function createNotConnectedError (
113
+ port : Port
114
+ ) : ApplicationError < number , PortDescriptor > {
115
+ const { protocol, protocolLabel, address, addressLabel } = port ;
116
+ return NotConnectedError (
117
+ nls . localize (
118
+ 'arduino/monitor/notConnectedError' ,
119
+ 'Not connected to {0} {1} port.' ,
120
+ addressLabel ,
121
+ protocolLabel
122
+ ) ,
123
+ { protocol, address }
124
+ ) ;
125
+ }
126
+ export function createAlreadyConnectedError (
127
+ port : Port
128
+ ) : ApplicationError < number , PortDescriptor > {
129
+ const { protocol, protocolLabel, address, addressLabel } = port ;
130
+ return AlreadyConnectedError (
131
+ nls . localize (
132
+ 'arduino/monitor/alreadyConnectedError' ,
133
+ 'Could not connect to {0} {1} port. Already connected.' ,
134
+ addressLabel ,
135
+ protocolLabel
136
+ ) ,
137
+ { protocol, address }
138
+ ) ;
139
+ }
140
+ export function createMissingConfigurationError (
141
+ port : Port
142
+ ) : ApplicationError < number , PortDescriptor > {
143
+ const { protocol, protocolLabel, address, addressLabel } = port ;
144
+ return MissingConfigurationError (
145
+ nls . localize (
146
+ 'arduino/monitor/missingConfigurationError' ,
147
+ 'Could not connect to {0} {1} port. The monitor configuration is missing.' ,
148
+ addressLabel ,
149
+ protocolLabel
150
+ ) ,
151
+ { protocol, address }
152
+ ) ;
153
+ }
154
+
155
+ /**
156
+ * Bare minimum representation of a port. Supports neither UI labels nor properties.
157
+ */
158
+ interface PortDescriptor {
159
+ readonly protocol : string ;
160
+ readonly address : string ;
161
+ }
162
+ function createMonitorError (
163
+ code : number
164
+ ) : ApplicationError . Constructor < number , PortDescriptor > {
165
+ return ApplicationError . declare (
166
+ code ,
167
+ ( message : string , data : PortDescriptor ) => ( { data, message } )
168
+ ) ;
95
169
}
0 commit comments