Skip to content

Commit

Permalink
feat: add middleware and errorHandling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEROM22 committed Jan 12, 2024
1 parent b905821 commit 5aee521
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
uploadFile
} = require("./zapier/files/actions.js")
const { beforeSessionAuthRequest } = require("./docspase/auth/auth.js")
const { errorHandling } = require("./middleware.js")
const {
fileCreated,
fileDeleted,
Expand Down Expand Up @@ -69,7 +70,10 @@ const App = {
[roomCreate.key]: roomCreate,
[shareRoom.key]: shareRoom,
[uploadFile.key]: uploadFile
}
},
afterResponse: [
errorHandling
]
}

module.exports = { App }
48 changes: 48 additions & 0 deletions app/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// (c) Copyright Ascensio System SIA 2023
//

// @ts-check

/**
* @typedef {import("zapier-platform-core").HttpResponse} HttpResponse
*/

/**
* @param {HttpResponse} response
* @param {ZObject} z
* @returns {HttpResponse}
*/
const errorHandling = (response, z) => {
if (response.status === 429) {
response.skipThrowForStatus = true
throw new z.errors.ThrottledError("Too many requests, Zapier will retry in 60 seconds", 60)
}

if ((response.status >= 400 && response.status <= 500)) {
response.skipThrowForStatus = true
const message = response.data.error?.message || response.data.message || "Unknown error"
const request = {
url: response.request.url,
method: response.request.method,
body: response.request.body
}
const stack = response.data.error?.stack || ""
z.console.log(`
Message: ${message}\n
Request:\n
url: ${request.url}\n
method: ${request.method}\n
body: ${request.body}\n
Stack: ${stack}
`)
// @ts-ignore TODO
const code = response.request.input?._zapier?.event?.method || request.method + " " + request.url
throw new z.errors.Error(message, code, response.data.statusCode)
}
return response
}

module.exports = {
errorHandling
}

0 comments on commit 5aee521

Please sign in to comment.