未分類

【kintone】【Node.js SDK】APIのErrorを見やすくする

環境

Node.js v12.4.0

Node.js SDK @kintone/kintone-js-sdk@0.7.4

課題

SDKのErrorオブジェクトをそのまま出力すると量が多くて見づらいです。

AddRecordのサンプルコードです。

addRecords.js


"use strict";
const kintone = require('@kintone/kintone-js-sdk'); const kintoneAuth = new kintone.Auth(); const paramsAuth = { username: '*********', password: '*********' }; kintoneAuth.setPasswordAuth(paramsAuth); const paramsConnection = { domain: '*********', auth: kintoneAuth }; const connection = new kintone.Connection(paramsConnection); const kintoneRecord = new kintone.Record({connection}); const app = 1; const record = { *********: { value: '*********' }, *********: { value: '*********' }, }; const records = [record]; kintoneRecord.addRecords({app, records}).then((rsp) => { console.log(rsp); }).catch((err) => { // This SDK return err with KintoneAPIException console.log(err); });

エラーが出るような設定で実行してみました。

出力が多すぎて見づらいですね。

実行結果


#addRecords.js

KintoneAPIException [Error]: Request failed with status code 520

at /Users/*********/*********/src/node_modules/@kintone/kintone-js-sdk/cjs/base/utils/Common.js:28:13

at processTicksAndRejections (internal/process/task_queues.js:89:5) {

originError: Error: Request failed with status code 520

at createError (/Users/*********/*********/src/node_modules/axios/lib/core/createError.js:16:15)

at settle (/Users/*********i/*********/src/node_modules/axios/lib/core/settle.js:17:12)

at IncomingMessage.handleStreamEnd (/Users/*********/*********/src/node_modules/axios/lib/adapters/http.js:237:11)

at IncomingMessage.emit (events.js:205:15)

at endReadableNT (_stream_readable.js:1154:12)

at processTicksAndRejections (internal/process/task_queues.js:84:9) {

config: {

url: 'https://*********:443/k/v1/records.json',

method: 'post',

data: '{"records":[{*********}',

headers: [Object],

proxy: false,

transformRequest: [Array],

transformResponse: [Array],

timeout: 0,

adapter: [Function: httpAdapter],

xsrfCookieName: 'XSRF-TOKEN',

xsrfHeaderName: 'X-XSRF-TOKEN',

maxContentLength: -1,

validateStatus: [Function: validateStatus]

},

request: ClientRequest {

_events: [Object: null prototype],

_eventsCount: 6,

_maxListeners: undefined,

outputData: [],

outputSize: 0,

writable: true,

_last: true,

chunkedEncoding: false,

shouldKeepAlive: false,

useChunkedEncodingByDefault: true,

sendDate: false,

_removedConnection: false,

_removedContLen: false,

_removedTE: false,

_contentLength: null,

_hasBody: true,

_trailer: '',

finished: true,

_headerSent: true,

socket: [TLSSocket],

connection: [TLSSocket],

_header: 'POST /k/v1/records.json HTTP/1.1\r' +

'\nAccept: application/json, text/plain, */*\r' +

'\nContent-Type: application/json;charset=utf-8\r' +

'\nX-Cybozu-Authorization: ' +

'*********r' +

'\nUser-Agent: @kintone/kintone-js-sdk/0.7.4\r' +

'\nContent-Length: 100\r' +

'\nHost:*********\r' +

'\nConnection: close\r' +

'\n\r' +

'\n',

_onPendingData: [Function: noopPendingOutput],

agent: [Agent],

socketPath: undefined,

method: 'POST',

path: '/k/v1/records.json',

_ended: true,

res: [IncomingMessage],

aborted: false,

timeoutCb: null,

upgradeOrConnect: false,

parser: null,

maxHeadersCount: null,

_redirectable: [Writable],

[Symbol(isCorked)]: false,

[Symbol(outHeadersKey)]: [Object: null prototype]

},

response: {

status: 520,

statusText: '520',

headers: [Object],

config: [Object],

request: [ClientRequest],

data: [Object]

},

isAxiosError: true,

toJSON: [Function]

},

httpErrorCode: 520,

errorResponse: ErrorResponse {

id: '*********',

code: 'GAIA_LO03',

message: 'ルックアップの参照先から値をコピーできません。「コピー元のフィールド」に指定したフィールドの設定で「値の重複を禁止する」を選択しておく必要があります。',

errors: undefined

}

}

解決方法

Errorオブジェクト(kintoneAPIException)で用意されているgetメソッドが便利です!

addRecord.js(変更点)

kintoneRecord.addRecords({app, records}).then((rsp) =>; {

console.log(rsp);

}).catch((err) =>; {

// This SDK return err with KintoneAPIException

console.log(err.get());

});

結果


{
id: 'OG5T488RTM8qUaTg4FhT',
code: 'GAIA_LO03',
message: 'ルックアップの参照先から値をコピーできません。「コピー元のフィールド」に指定したフィールドの設定で「値の重複を禁止する」を選択しておく必要があります。',
errors: null
}