Extrac ErrorsParser to separate class and make it handle the rails error structure with keys

This commit is contained in:
Luis Ramos
2020-02-10 13:51:44 +00:00
parent 9d8608f210
commit 7639e9a38d
3 changed files with 38 additions and 13 deletions

View File

@@ -0,0 +1,17 @@
# Parses a structure of errors that came from the server
angular.module("admin.utils").factory "ErrorsParser", ->
new class ErrorsParser
toString: (errors, defaultContent = "") =>
if errors.length > 0
# it is an array of errors
errorsString = error + "\n" for error in errors
else
# it is a hash of errors
keys = Object.keys(errors)
errorsString = ""
for key in keys
errorsString += error for error in errors[key]
errorsString = defaultContent if errorsString == ""
errorsString