TMVCActiveRecord捕获异常信息,自定义输出

redhat588 / 2023-05-03 / 原文

  TMVCActiveRecord.CurrentConnection.StartTransaction;
  try
    //do
    TMVCActiveRecord.CurrentConnection.Commit;
    Render(201, 'Create Successfully', '');
  except
    on E: Exception do
    begin
      TMVCActiveRecord.CurrentConnection.Rollback;
      if E.Message.Contains('不能在对象 ''dbo.MOCTC'' 中插入重复键。') then
      begin
        raise EMVCException.Create('Slipcode already exists!', '', 0, 404);
      end
      else
        raise EMVCException.Create(E.Message, '', 0, 404);
    end;
  end;