|
1 | 1 | /// <reference path="typings/tsd.d.ts" /> |
2 | 2 |
|
3 | 3 | // TODO: We'll need a poly fill for promises. |
| 4 | +// TODO: Process Errors |
| 5 | +// TODO: Handle Server Settings |
| 6 | + |
4 | 7 | module Exceptionless { |
5 | 8 | export class ExceptionlessClient { |
6 | 9 | public config:Configuration; |
@@ -206,58 +209,73 @@ module Exceptionless { |
206 | 209 | this._config.log.info('Sending ' + events.length + ' events to ' + this._config.serverUrl + '.'); |
207 | 210 | this._config.submissionClient.submit(events, this._config) |
208 | 211 | .then( |
209 | | - (response:SubmissionResponse) => { |
210 | | - if (response.success) { |
211 | | - this._config.log.info('Sent ' + events.length + ' events to ' + this._config.serverUrl + '.'); |
212 | | - } else if (response.serviceUnavailable) { |
213 | | - // You are currently over your rate limit or the servers are under stress. |
214 | | - this._config.log.error('Server returned service unavailable.'); |
215 | | - this.suspendProcessing(); |
216 | | - this.requeueEvents(events); |
217 | | - } else if (response.paymentRequired) { |
218 | | - // If the organization over the rate limit then discard the event. |
219 | | - this._config.log.info('Too many events have been submitted, please upgrade your plan.'); |
220 | | - this.suspendProcessing(null, true, true); |
221 | | - } else if (response.unableToAuthenticate) { |
222 | | - // The api key was suspended or could not be authorized. |
223 | | - this._config.log.info('Unable to authenticate, please check your configuration. The event will not be submitted.'); |
224 | | - this.suspendProcessing(15); |
225 | | - } else if (response.notFound || response.badRequest) { |
226 | | - // The service end point could not be found. |
227 | | - this._config.log.error('Error while trying to submit data: ' + response.message); |
228 | | - this.suspendProcessing(60 * 4); |
229 | | - } else if (response.requestEntityTooLarge) { |
230 | | - if (this._config.submissionBatchSize > 1) { |
231 | | - this._config.log.error('Event submission discarded for being too large. The event will be retried with a smaller events size.'); |
232 | | - this._config.submissionBatchSize = Math.max(1, Math.round(this._config.submissionBatchSize / 1.5)); |
233 | | - this.requeueEvents(events); |
234 | | - } else { |
235 | | - this._config.log.error('Event submission discarded for being too large. The event will not be submitted.'); |
236 | | - } |
237 | | - } else if (!response.success) { |
238 | | - this._config.log.error('An error occurred while submitting events: ' + response.message); |
239 | | - this.suspendProcessing(); |
240 | | - this.requeueEvents(events); |
241 | | - } |
242 | | - }, |
243 | | - (response:SubmissionResponse) => { |
244 | | - this._config.log.error('An error occurred while submitting events: ' + response.message); |
245 | | - this.suspendProcessing(); |
246 | | - this.requeueEvents(events); |
247 | | - }) |
248 | | - .then(() => { |
249 | | - this._config.log.info('Finished processing queue.'); |
250 | | - this._processingQueue = false; |
251 | | - }); |
| 212 | + (response:SubmissionResponse) => this.processSubmissionResponse(response, events), |
| 213 | + (response:SubmissionResponse) => this.processSubmissionResponse(response, events)) |
| 214 | + .then(() => { |
| 215 | + this._config.log.info('Finished processing queue.'); |
| 216 | + this._processingQueue = false; |
| 217 | + }); |
252 | 218 | } catch (ex) { |
253 | 219 | this._config.log.error('An error occurred while processing the queue: ' + ex); |
254 | 220 | this.suspendProcessing(); |
255 | | - } finally { |
256 | | - this._config.log.info('Finished processing queue.'); |
257 | 221 | this._processingQueue = false; |
258 | 222 | } |
259 | 223 | } |
260 | 224 |
|
| 225 | + private processSubmissionResponse(response:SubmissionResponse, events:IEvent[]){ |
| 226 | + if (response.success) { |
| 227 | + this._config.log.info('Sent ' + events.length + ' events to ' + this._config.serverUrl + '.'); |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + if (response.serviceUnavailable) { |
| 232 | + // You are currently over your rate limit or the servers are under stress. |
| 233 | + this._config.log.error('Server returned service unavailable.'); |
| 234 | + this.suspendProcessing(); |
| 235 | + this.requeueEvents(events); |
| 236 | + return; |
| 237 | + } |
| 238 | + |
| 239 | + if (response.paymentRequired) { |
| 240 | + // If the organization over the rate limit then discard the event. |
| 241 | + this._config.log.info('Too many events have been submitted, please upgrade your plan.'); |
| 242 | + this.suspendProcessing(null, true, true); |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + if (response.unableToAuthenticate) { |
| 247 | + // The api key was suspended or could not be authorized. |
| 248 | + this._config.log.info('Unable to authenticate, please check your configuration. The event will not be submitted.'); |
| 249 | + this.suspendProcessing(15); |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + if (response.notFound || response.badRequest) { |
| 254 | + // The service end point could not be found. |
| 255 | + this._config.log.error('Error while trying to submit data: ' + response.message); |
| 256 | + this.suspendProcessing(60 * 4); |
| 257 | + return; |
| 258 | + } |
| 259 | + |
| 260 | + if (response.requestEntityTooLarge) { |
| 261 | + if (this._config.submissionBatchSize > 1) { |
| 262 | + this._config.log.error('Event submission discarded for being too large. The event will be retried with a smaller events size.'); |
| 263 | + this._config.submissionBatchSize = Math.max(1, Math.round(this._config.submissionBatchSize / 1.5)); |
| 264 | + this.requeueEvents(events); |
| 265 | + } else { |
| 266 | + this._config.log.error('Event submission discarded for being too large. The event will not be submitted.'); |
| 267 | + } |
| 268 | + |
| 269 | + return; |
| 270 | + } |
| 271 | + |
| 272 | + if (!response.success) { |
| 273 | + this._config.log.error('An error occurred while submitting events: ' + response.message); |
| 274 | + this.suspendProcessing(); |
| 275 | + this.requeueEvents(events); |
| 276 | + } |
| 277 | + } |
| 278 | + |
261 | 279 | private onProcessQueue() { |
262 | 280 | if (!this.isQueueProcessingSuspended() && !this._processingQueue) { |
263 | 281 | this.process(); |
|
0 commit comments