cht-conf icon indicating copy to clipboard operation
cht-conf copied to clipboard

Validation API fails unexpectedly with incorrect URL

Open mrjones-plip opened this issue 3 years ago • 0 comments

What feature do you want to improve? I was using cht-conf to try and upload my app and accidentally left off the s in my http://localhost URL. The error handling of this was poor such that it took me some time to debug my one character mistake.

Describe the improvement you'd like We should try and better detect when a user is using a non-CHT URL. When running the docker instance of CHT from the helper script, you could easily miss the s in https as I did, and then you're sent to nginx's protocol mismatch page (400 - HTTP request was sent to HTTPS port). This gave me this error:

➜  default git:(master) ✗ cht --url=http://localhost:8443  upload-app-forms -- pregnancy
INFO Processing config in default. 
INFO Actions:
     - upload-app-forms 
INFO Starting action: upload-app-forms… 
INFO Validating form: pregnancy.xml… 
ERROR Error found while validating "/home/mrjones/Documents/MedicMobile/cht-core/config/default/forms/app/pregnancy.xml". Validation response: Unexpected token < in JSON at position 0 
ERROR Error: One or more forms appears to have errors found by the API validation endpoint.
    at module.exports (/usr/lib/node_modules/cht-conf/src/lib/validate-forms.js:51:11)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) 

cht-conf is expecting JSON back and instead gets HTML which it then tries to parse and the very first character of the response is < which is invalid JSON, hence Unexpected token < in JSON at position 0.

Describe alternatives you've considered Don't fat finger the URL again ;)

Additional context This doesn't just apply to the validation API I guess, as the forms fail to upload too if you get past that. For example, when we go to a really wrong URL, like google.com, you see the validation fails with a strong hint that you have the wrong URL, but then it tries to upload the form anyway:

cht --url=http://google.com  upload-app-forms -- pregnancy
INFO Processing config in default. 
INFO Actions:
     - upload-app-forms 
INFO Starting action: upload-app-forms… 
INFO Validating form: pregnancy.xml… 
WARN Form validation endpoint not found in your version of CHT Core, no form will be checked before push 
INFO Preparing form for upload: pregnancy.xml… 
INFO No media directory found at /home/mrjones/Documents/MedicMobile/cht-core/config/default/forms/app/pregnancy-media for form /home/mrjones/Documents/MedicMobile/cht-core/config/default/forms/app/pregnancy.xml 
ERROR Error: Unable to fetch xml attachment of doc with id form:pregnancy, returned status code = undefined
    at Object.preUploadForm (/usr/lib/node_modules/cht-conf/src/lib/warn-upload-overwrite.js:199:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

mrjones-plip avatar Oct 12 '21 05:10 mrjones-plip