try-parser.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. /** 语句:try:须含 try、success、fail 三个数组(可为 []);可选 continueAfterFail */
  2. const { assertStrictKeys } = require('../action-schema.js')
  3. const types = ['try']
  4. function parse (action, parseContext) {
  5. const path = parseContext.actionPath || 'try'
  6. assertStrictKeys(action, ['type', 'try', 'success', 'fail', 'continueAfterFail'], path)
  7. if (!Array.isArray(action.try)) {
  8. throw new Error(`${path}: try 须包含数组 try`)
  9. }
  10. if (!Array.isArray(action.success)) {
  11. throw new Error(`${path}: try 须包含数组 success(无成功后续写 [])`)
  12. }
  13. if (!Array.isArray(action.fail)) {
  14. throw new Error(`${path}: try 须包含数组 fail(无失败处理写 [])`)
  15. }
  16. const out = {
  17. type: 'try',
  18. try: parseContext.parseActions(action.try, 'try'),
  19. success: parseContext.parseActions(action.success, 'success'),
  20. fail: parseContext.parseActions(action.fail, 'fail'),
  21. }
  22. if (action.continueAfterFail === true) out.continueAfterFail = true
  23. return out
  24. }
  25. async function execute() {
  26. return { success: true }
  27. }
  28. module.exports = { types, parse, execute }