| 
									
										
										
										
											2018-02-21 14:41:05 -08:00
										 |  |  | import { apiErrorStatusMessage } from 'wherehows-web/constants/errors/errors'; | 
					
						
							| 
									
										
										
										
											2018-02-22 14:26:19 -08:00
										 |  |  | import { ApiResponseStatus } from 'wherehows-web/utils/api'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Extends the built-in Error class with attributes related to treating non 200 OK responses | 
					
						
							|  |  |  |  * at the api layer as exceptions | 
					
						
							|  |  |  |  * @class ApiError | 
					
						
							|  |  |  |  * @extends {Error} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ApiError extends Error { | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Timestamp of when the exception occurred | 
					
						
							|  |  |  |    * @readonly | 
					
						
							|  |  |  |    * @memberof ApiError | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   readonly timestamp = new Date(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(readonly status: ApiResponseStatus, message: string, ...args: Array<any>) { | 
					
						
							|  |  |  |     super(...[message, ...args]); | 
					
						
							|  |  |  |     // Fixes downlevel compiler limitation with correct prototype chain adjustment
 | 
					
						
							|  |  |  |     // i.e. ensuring this is also `instanceof` subclass
 | 
					
						
							|  |  |  |     Object.setPrototypeOf(this, ApiError.prototype); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-21 14:41:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Wraps a Response object, pass through json response if no api error, | 
					
						
							|  |  |  |  * otherwise raise exception with error message | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Response} response | 
					
						
							|  |  |  |  * @returns {Promise<T>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const throwIfApiError = async <T>(response: Response): Promise<T> => { | 
					
						
							|  |  |  |   const { status, ok } = response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!ok) { | 
					
						
							|  |  |  |     const { msg = apiErrorStatusMessage(status) } = await response.json(); | 
					
						
							| 
									
										
										
										
											2018-02-22 14:26:19 -08:00
										 |  |  |     throw new ApiError(status, msg); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:41:05 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return response.json(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 14:26:19 -08:00
										 |  |  | export { throwIfApiError, ApiError }; |