Browse Source

fix: test webhook throw error on failure and hook_log corrections

re #1903

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1917/head
Pranav C 2 years ago
parent
commit
ea5a07d134
  1. 8
      packages/nc-gui/components/project/tableTabs/webhooksTest.vue
  2. 26
      packages/nocodb/src/lib/noco-models/HookLog.ts
  3. 3
      packages/nocodb/src/lib/noco/meta/api/hookApis.ts
  4. 16
      packages/nocodb/src/lib/noco/meta/helpers/webhookHelpers.ts

8
packages/nc-gui/components/project/tableTabs/webhooksTest.vue

@ -38,15 +38,15 @@ export default {
},
async testWebhook() {
try {
const res = await this.$api.dbTableWebhook.test(this.modelId, {
await this.$api.dbTableWebhook.test(this.modelId, {
hook: this.hook,
payload: this.sampleData
})
this.$toast.success('Webhook tested successfully').goAway(3000)
} catch (_e) {
const e = await this._extractSdkResponseError(_e)
this.$toast.error(e.message).goAway(3000)
} catch (e) {
const msg = await this._extractSdkResponseErrorMsg(e)
this.$toast.error(msg).goAway(3000)
}
}
}

26
packages/nocodb/src/lib/noco-models/HookLog.ts

@ -66,14 +66,14 @@ export default class HookLog implements HookLogType {
public static async insert(
hookLog: Partial<
(HookLog | HookLogType) & {
HookLog & {
created_at?;
updated_at?;
}
>,
ncMeta = Noco.ncMeta
) {
const insertObj = extractProps(hookLog, [
const insertObj: any = extractProps(hookLog, [
'base_id',
'project_id',
'fk_hook_id',
@ -98,24 +98,12 @@ export default class HookLog implements HookLogType {
insertObj.base_id = hook.base_id;
}
return await ncMeta.metaInsert2(null, null, MetaTable.HOOK_LOGS, insertObj);
if (typeof insertObj.notification === 'object') {
insertObj.notification = JSON.stringify(insertObj.notification);
}
// todo: redis cache ??
// await NocoCache.appendToList(
// CacheScope.HOOK,
// [insertObj.fk_mo_id],
// `${CacheScope.HOOK}:${id}`
// );
insertObj.execution_time = parseInt(insertObj.execution_time) || 0;
// return this.get(id, ncMeta);
return await ncMeta.metaInsert2(null, null, MetaTable.HOOK_LOGS, insertObj);
}
// static async delete(hookId: any, ncMeta = Noco.ncMeta) {
// await NocoCache.deepDel(
// CacheScope.HOOK,
// `${CacheScope.HOOK}:${hookId}`,
// CacheDelDirection.CHILD_TO_PARENT
// );
// return await ncMeta.metaDelete(null, null, MetaTable.HOOKS, hookId);
// }
}

3
packages/nocodb/src/lib/noco/meta/api/hookApis.ts

@ -60,7 +60,8 @@ export async function hookTest(req: Request<any, any>, res: Response) {
model,
data,
user,
(hook as any)?.filters
(hook as any)?.filters,
true
);
Tele.emit('evt', { evt_type: 'webhooks:tested' });

16
packages/nocodb/src/lib/noco/meta/helpers/webhookHelpers.ts

@ -188,7 +188,8 @@ export async function invokeWebhook(
_model: Model,
data,
user,
testFilters = null
testFilters = null,
throwErrorOnFailure = false
) {
let hookLog: HookLogType;
const startTime = process.hrtime();
@ -289,16 +290,19 @@ export async function invokeWebhook(
}
} catch (e) {
console.log(e);
hookLog = {
...hook,
error_code: e.error_code,
error_message: e.message,
error: JSON.stringify(e)
};
if (throwErrorOnFailure) throw e;
} finally {
hookLog.execution_time = parseHrtimeToMilliSeconds(
process.hrtime(startTime)
);
if (hookLog) HookLog.insert({ ...hookLog, test_call: !!testFilters });
}
hookLog.execution_time = parseHrtimeToMilliSeconds(process.hrtime(startTime));
if (hookLog) await HookLog.insert({ ...hookLog, test_call: !!testFilters });
}
export function _transformSubmittedFormDataForEmail(
@ -342,6 +346,6 @@ export function _transformSubmittedFormDataForEmail(
}
function parseHrtimeToMilliSeconds(hrtime) {
const seconds = (hrtime[0] + hrtime[1] / 1e6).toFixed(3);
return seconds;
const milliseconds = (hrtime[0] + hrtime[1] / 1e6).toFixed(3);
return milliseconds;
}

Loading…
Cancel
Save