| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require_relative 'devops_error' | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   module Exception | 
					
						
							|  |  |  |     class ValidationError < DevopsError | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       attr_accessor :body | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def self.create_from_db_exception e | 
					
						
							|  |  |  |         msgs = e.document.errors.messages | 
					
						
							|  |  |  |         create_from_messages(msgs) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.create_from_messages msgs | 
					
						
							|  |  |  |         msgs.keys.each{|k| msgs[k] = msgs[k][0]} | 
					
						
							|  |  |  |         Devops::Exception::ValidationError.new(nil, msgs) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize msg, body={} | 
					
						
							|  |  |  |         super(msg || "Validation error") | 
					
						
							|  |  |  |         self.body = body | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def http_status | 
					
						
							|  |  |  |         422
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def http_body | 
					
						
							|  |  |  |         JSON.pretty_generate(self.body.merge(message: self.message)) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  | end |