kintone.apiでREST APIを実行した結果がエラーとなる原因調査

お世話になっております。
樋口です。

現在、とあるアプリの保存時に、別アプリの項目を自動計算するカスタマイズをjsで実装しています。

その際にレコード更新api(https://cybozudev.zendesk.com/hc/ja/articles/201941784-レコードの更新-PUT-)を利用しているのですが、実行結果がエラーと判定されているにもかかわらず、戻り値のerrorオブジェクトは空であり、かつHTMLコードも200となっています。

このようなエラーについて、どのように対処すればよいでしょうか。

よろしくお願いいたします。

・実際のjavascript

(function () {
“use strict”;

var app = 29;
var targetApp = 30;

/*
 * 支出保存時のエラーチェック+自動計算
 */
var balance_autoCalc = function(event) {

    // 入力されている顧客IDと契約カテゴリを取得する
    var customer_id = "";
    var category = "";

    var records = event.record;

    if (!records) return event;

    if(records.customer_id.value != undefined) customer_id = records.customer_id.value;
    if(records.category.value != undefined) category = records.category.value;

    // ルックアップで取得された顧客IDと契約カテゴリを取得する
    var agreement_customer_id = "";
    var agreement_category = "";

    if(records.agreement_customer_id.value != undefined) agreement_customer_id = records.agreement_customer_id.value;
    if(records.agreement_category.value != undefined) agreement_category = records.agreement_category.value;

    // 支出表で洗濯された顧客と契約カテゴリが、参照された契約と一致しているかどうかを判定する
    if(customer_id != "" && agreement_customer_id != "" && customer_id != agreement_customer_id) {
        records['agreement_customer']['error'] = '「契約顧客」と「顧客」を揃えてください。';
        return event;
    }
    if(category != "" && agreement_category != "" && category != agreement_category) {
        records['agreement_category']['error'] = '「契約カテゴリ」と「カテゴリ」を揃えてください。';
        return event;
    }

    // 残額自動計算の有無を確認する
    var agreement_id = 0;
    if(records.agreement_id.value != undefined && records.agreement_id.value != "") {
        agreement_id = parseInt(records.agreement_id.value, 10);
    }
    if(agreement_id == 0) return event;

    // 残額の自動計算を実施
    var payment = 0;
    var agreement_balance = 0;

    if(records.payment.value != undefined && records.payment.value != "") {
        payment = parseInt(records.payment.value, 10);
    }
    if(records.agreement_balance.value != undefined && records.agreement_balance.value != "") {
        agreement_balance = parseInt(records.agreement_balance.value, 10);
    }

    var current_balance = agreement_balance - payment;
    if(current_balance < 0) {
        records['payment']['error'] = '残金額より多く支払われています。';
        return event;
    }

    var jsondata = {
        "app": targetApp,
        "id" : agreement_id,
        "record": {
            "common_balance_payable": {
                "value": current_balance
            }
        }
    };
    alert("app:" + targetApp + " id:" + agreement_id + " balance:" + current_balance);
    kintone.api(kintone.api.url("/k/v1/record"), "PUT", jsondata)
    .then(function(resp) {
            alert('success');
            return event;
        })
    .catch(function(error) {
            // エラーの場合はメッセージを表示する
            var errmsg = "残額の自動計算に失敗しました。契約アプリ側を手動で更新してください。";      
            // レスポンスにエラーメッセージが含まれる場合はメッセージを表示する
            if (error.message !== undefined){
                errmsg += "" + error.message;
            }
            alert(errmsg);
            return event;
        });

    return event;

};

// 保存時のエラーチェックを行う
kintone.events.on(["app.record.create.submit","app.record.edit.submit"], function (event) {
    return balance_autoCalc(event);
});

})();

発生しているエラーの状況がいまいち把握できていないのですが、
kintoneAPI側の処理でエラーが返ってきているが(catch(function(error)
の処理が実行されている)、リクエストのレスポンスは200が返ってきているという
状況でしょうか?

ちなみにレコードの更新自体もされていないという認識であっていますか?
ソースのPUT処理部分自体には問題が内容に見受けられますが。

おっしゃるとおり、以下の状態となっています。

1.リクエストのレスポンスは200で返ってきている
2.APIの処理はエラーで返されており、catch以下が実行されている

ただし次の点が不可解なのですが、「レコードの更新自体は実行されている」状態となっています。

公式のサンプルにはPromise.prototype.then(onFulfilled, onRejected)で成功時と失敗時のイベントを両方指定する例しかのっていないので、
もしかしたらPromise.prototype.catchは未定義なのかもしれません。
then()の第2引数で指定した場合はどうなりますか?
catchが必ず発生するようなら不具合になると思います。

ご指摘いただいた方法で実装致しました。
結果として、「catchが発生したり発生しなかったりする」という謎の事態となりました。

catchが発生した場合も、自動計算とレコードの更新は実行されています。

発生する、しないでは上記ソース中の「顧客」「カテゴリ」・「契約顧客」「契約カテゴリ」が異なるのですが、処理としては同様のものとなるはずです。

また、レコード新規追加時にのみcatchが発生し、全く同じレコードを「更新」した際には正常に処理が完了致しました。

すいません、自己解決致しました。
どうやらkintone.apiが完了する前にsubmitイベントが完了してしまったことが原因でおかしくなっていたようです。

ご回答いただきましてありがとうございました。