Skip to content

Commit

Permalink
[HOTFIX][FE]: Add more error handling for unindentified response from…
Browse files Browse the repository at this point in the history
… API (#131)
  • Loading branch information
Nicklas373 committed May 12, 2024
2 parents 5a042d5 + 326a714 commit a67c1f8
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 47 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ used of Flowbite library to maintain responsive and materialize interface. And p
- [Docker Container - docker/master](https://github.com/Nicklas373/Hana-PDF/tree/docker/master)
- [Frontend Services - fe/master](https://github.com/Nicklas373/Hana-PDF/tree/fe/master)
- [Backend Services - be/master](https://github.com/Nicklas373/Hana-PDF/tree/be/master)
- [(DEV) Frontend Services - dev/fe/master](https://github.com/Nicklas373/Hana-PDF/tree/dev/fe/master)
- [(DEV) Backend Services - dev/be/master](https://github.com/Nicklas373/Hana-PDF/tree/dev/be/master)

---

Expand All @@ -55,12 +57,13 @@ used of Flowbite library to maintain responsive and materialize interface. And p
2. Start to deploy
````bash
- docker compose up -d
- docker compose exec hana-api-service php artisan migrate
- docker compose exec hana-api-service php artisan db:seed
- docker compose exec hana-api-services php artisan migrate
- docker compose exec hana-api-services php artisan db:seed
````
3. Configure Server Host
````bash
- docker compose exec hana-api-services echo "TELEGRAM_BOT_ID=YOUR_TELEGRAM_BOT_ID" >> .env
- docker compose exec hana-api-services echo "TELEGRAM_TASK_ID=YOUR_TELEGRAM_CHANNEL_ID" >> .env
- docker compose exec hana-api-services echo "TELEGRAM_CHAT_ID=YOUR_TELEGRAM_CHANNEL_ID" >> .env
- docker compose exec hana-api-services echo "HANA_UNIQUE_TOKEN=YOUR_SHA512_UNIQUE_TOKEN" >> .env
- docker compose exec hana-api-services sed -i "s/ASPOSE_CLOUD_CLIENT_ID=xxxx/ASPOSE_CLOUD_CLIENT_ID=YOUR_ASPOSE_CLOUD_CLIENT_ID/" >> .env
Expand Down
28 changes: 0 additions & 28 deletions public/build/assets/kao-logic-Bp6_TJmE.js

This file was deleted.

28 changes: 28 additions & 0 deletions public/build/assets/kao-logic-DB00_MMO.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
]
},
"resources/js/kao-logic.js": {
"file": "assets/kao-logic-Bp6_TJmE.js",
"file": "assets/kao-logic-DB00_MMO.js",
"name": "kao-logic",
"src": "resources/js/kao-logic.js",
"isEntry": true,
Expand Down
123 changes: 109 additions & 14 deletions resources/js/kao-logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ if (procBtn) {
errSubMessage.innerText = ""
errListTitleMessage.innerText = "Error message"
resetErrListMessage()
generateMesssage(error)
generateMesssage(error.xhrBalanceStatus+" - "+error.xhrBalanceResponse)
errAltSubMessageModal.style = null
loadingModal.hide()
errModal.show()
Expand Down Expand Up @@ -848,8 +848,15 @@ function apiGateway(proc, action) {
sendToAPI(files,proc,action).then(function () {
loadingModal.hide()
}).catch(function (error) {
errModal.hide()
errMessage.innerText = "There was unexpected error !"
errSubMessage.innerText = ""
errListTitleMessage.innerText = "Error message"
resetErrListMessage()
generateMesssage(error.xhrRequestStatus+" - "+error.xhrRequestMessage)
errAltSubMessageModal.style = null
loadingModal.hide()
console.error(error)
errModal.show()
})
}

Expand Down Expand Up @@ -882,6 +889,8 @@ function sendToAPI(files, proc, action) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest()
var formData = new FormData()
var timerStart = Date.now()

if (proc == 'compress') {
var compMethodValue = document.querySelector('input[name="compMethod"]:checked').value
formData.append('compMethod', compMethodValue)
Expand Down Expand Up @@ -1042,10 +1051,25 @@ function sendToAPI(files, proc, action) {
formData.append('file[' + index + ']', file)
})
}

var timer = setInterval(function() {
if (Date.now() - timerStart > 105000) {
xhr.abort()
clearInterval(timer)
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Connection timeout',
xhrRequestServerMessage: '',
xhrRequestStatus: 524
})
}
}, 1000)

xhr.open('POST', apiUrl+'/api/v1/core/'+proc, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + bearerToken)
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
clearInterval(timerStart)
if (xhr.responseText.trim().startsWith('{')) {
var xhrReturn = JSON.parse(xhr.responseText)
if (xhr.status == 200) {
Expand All @@ -1061,15 +1085,25 @@ function sendToAPI(files, proc, action) {
`
document.getElementById("scsMsgLink").href = apiUrl+xhrReturn.fileSource
document.getElementById("scsMsgLink").innerText = "Download PDF"
resolve()
resolve({
xhrRequestCondition: 'OK',
xhrRequestMessage: xhrReturn.message,
xhrRequestServerMessage: '',
xhrRequestStatus: xhr.status
})
} else {
document.getElementById("alert-scs").classList.remove("hidden","opacity-0")
document.getElementById("alert-err").classList.add("hidden","opacity-0")
document.getElementById("scsMsgTitle").innerText = `HANA PDF Process completed !`
document.getElementById("scsMsgResult").innerText = `Download the file or PDF below.`
document.getElementById("scsMsgLink").href = apiUrl+xhrReturn.fileSource
document.getElementById("scsMsgLink").innerText = "Download PDF"
resolve()
resolve({
xhrRequestCondition: 'OK',
xhrRequestMessage: xhrReturn.message,
xhrRequestServerMessage: '',
xhrRequestStatus: xhr.status
})
}
} else {
document.getElementById("alert-scs").classList.add("hidden","opacity-0")
Expand All @@ -1078,7 +1112,12 @@ function sendToAPI(files, proc, action) {
document.getElementById("errMsg").innerText = xhrReturn.message
document.getElementById("errProcMain").classList.remove("hidden")
document.getElementById("errProcId").innerText = xhrReturn.processId
reject(new Error('API response: '+xhrReturn.errors+' !')) //Force stop loadingModal while return error !
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Response are not return 200 !',
xhrRequestServerMessage: xhrReturn.errors,
xhrRequestStatus: xhr.status
})
}
} else {
document.getElementById("alert-scs").classList.add("hidden","opacity-0")
Expand All @@ -1087,16 +1126,37 @@ function sendToAPI(files, proc, action) {
document.getElementById("errMsg").innerText = xhrReturn.message
document.getElementById("errProcMain").classList.remove("hidden")
document.getElementById("errProcId").innerText = xhrReturn.processId
reject(new Error('API response: '+xhrReturn.status+' !')) //Force stop loadingModal while return error !
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Response are not return 200 !',
xhrRequestServerMessage: xhrReturn.message,
xhrRequestStatus: xhr.status
})
}
} else if (xhr.status == 524) {
document.getElementById("alert-scs").classList.add("hidden","opacity-0")
document.getElementById("alert-err").classList.remove("hidden","opacity-0")
document.getElementById("errMsgTitle").innerText = "HANA PDF Process failed !"
document.getElementById("errMsg").innerText = "There was unexpected error !, please try again later."
document.getElementById("errProcMain").classList.remove("hidden")
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Internal server error !',
xhrRequestServerMessage: '',
xhrRequestStatus: xhr.status
})
} else {
document.getElementById("alert-scs").classList.add("hidden","opacity-0")
document.getElementById("alert-err").classList.remove("hidden","opacity-0")
document.getElementById("errMsgTitle").innerText = "HANA PDF Process failed !"
document.getElementById("errMsg").innerText = xhrReturn.message
document.getElementById("errMsg").innerText = "There was unexpected error !, please try again later."
document.getElementById("errProcMain").classList.remove("hidden")
document.getElementById("errProcId").innerText = xhrReturn.processId
reject(new Error('API response: '+xhr.status+' !')) //Force stop loadingModal while return error !
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Internal server error !',
xhrRequestServerMessage: '',
xhrRequestStatus: xhr.status
})
}
}
} else {
Expand All @@ -1105,8 +1165,12 @@ function sendToAPI(files, proc, action) {
document.getElementById("errMsgTitle").innerText = "HANA PDF Process failed !"
document.getElementById("errMsg").innerText = "There was unexpected error !, please try again later."
document.getElementById("errProcMain").classList.add("hidden")
reject(new Error('API response error !'))
loadingModal.hide() //Force stop loadingModal while return error !
reject({
xhrRequestCondition: 'ERROR',
xhrRequestMessage: 'Server are not in readyState ! ('+xhr.readyState+')',
xhrRequestServerMessage: '',
xhrRequestStatus: 0
})
}
}
xhr.send(formData)
Expand Down Expand Up @@ -1858,21 +1922,52 @@ function remainingBalance() {
if (xhrReturn.status == 200) {
if (xhrReturn.remaining > 0) {
xhrBalance = true
xhrBalanceRemaining = xhrReturn.remaining
resolve({
xhrBalance: true,
xhrBalanceRemaining: xhrReturn.remaining,
xhrBalanceStatus: xhrReturn.status,
xhrBalanceResponse: xhrReturn.message
})
} else {
xhrBalance = false
xhrBalanceRemaining = xhrReturn.remaining
resolve({
xhrBalance: false,
xhrBalanceRemaining: xhrReturn.remaining,
xhrBalanceStatus: xhrReturn.status,
xhrBalanceResponse: xhrReturn.message
})
}
xhrBalanceRemaining = xhrReturn.remaining
} else {
xhrBalance = false
xhrBalanceRemaining = 0
resolve({
xhrBalance: false,
xhrBalanceRemaining: 0,
xhrBalanceStatus: xhrReturn.status,
xhrBalanceResponse: xhrReturn.message
})
}
resolve()
} else if (xhr.status == 429) {
xhrBalance = false
reject(new Error('Too many request'))
xhrBalanceRemaining = 0
reject({
xhrBalance: false,
xhrBalanceRemaining: 0,
xhrBalanceStatus: 429,
xhrBalanceResponse: 'Too many request'
})
} else {
xhrBalance = false
reject(new Error('Failed to fetch monthly limit'))
xhrBalanceRemaining = 0
reject({
xhrBalance: false,
xhrBalanceRemaining: 0,
xhrBalanceStatus: xhr.status,
xhrBalanceResponse: 'Failed to fetch monthly limit'
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/alternate-layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div id="content" class="flex flex-col min-h-screen justify-between">
@yield('content')
</div>
<script async type="module" src="{{asset('build/assets/kao-logic-Bp6_TJmE.js')}}"></script>
<script async type="module" src="{{asset('build/assets/kao-logic-DB00_MMO.js')}}"></script>
<script async type="module" src="https://unpkg.com/pdfjs-dist@4.0.379/build/pdf.mjs"></script>
<script async type="module" src="https://unpkg.com/pdfjs-dist@4.0.379/build/pdf.worker.mjs"></script>
<script async type="text/javascript" src="{{asset('ext-js/kao-controller.js')}}"></script>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/main-layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div id="content" class="flex flex-col min-h-screen justify-between">
@yield('content')
</div>
<script async type="module" src="{{asset('build/assets/kao-logic-Bp6_TJmE.js')}}"></script>
<script async type="module" src="{{asset('build/assets/kao-logic-DB00_MMO.js')}}"></script>
<script async type="text/javascript" src="{{asset('ext-js/kao-controller.js')}}"></script>
<script async type="text/javascript" src="{{asset('ext-js/kao-main.js')}}"></script>
<script async type="text/javascript" src="https://unpkg.com/@material-tailwind/html@latest/scripts/ripple.js"></script>
Expand Down

0 comments on commit a67c1f8

Please sign in to comment.