Commit 8ea2305a03ccb6ed8dbc25b68a3ccb9deeb0c063
1 parent
4e75594977
Exists in
master
Perbaikan dump request
Showing 2 changed files with 2 additions and 1 deletions Inline Diff
lib/dumper/request.js
1 | const MODULE_NAME = 'DUMPER.REQUEST'; | 1 | const MODULE_NAME = 'DUMPER.REQUEST'; |
2 | 2 | ||
3 | const fs = require('fs'); | 3 | const fs = require('fs'); |
4 | 4 | ||
5 | const fsPromise = fs.promises; | 5 | const fsPromise = fs.promises; |
6 | const moment = require('moment'); | 6 | const moment = require('moment'); |
7 | const config = require('komodo-sdk/config'); | 7 | const config = require('komodo-sdk/config'); |
8 | const logger = require('komodo-sdk/logger'); | 8 | const logger = require('komodo-sdk/logger'); |
9 | const path = require('path'); | 9 | const path = require('path'); |
10 | const mkdirIfNotExists = require('../mkdir-if-not-exists'); | 10 | const mkdirIfNotExists = require('../mkdir-if-not-exists'); |
11 | 11 | ||
12 | require('./init'); | 12 | require('./init'); |
13 | const vars = require('./vars'); | 13 | const vars = require('./vars'); |
14 | 14 | ||
15 | const { baseDumpDir } = vars; | 15 | const { baseDumpDir } = vars; |
16 | const baseRequestDumpDir = path.join(baseDumpDir, 'request'); | 16 | const baseRequestDumpDir = path.join(baseDumpDir, 'request'); |
17 | 17 | ||
18 | if (!fs.existsSync(baseRequestDumpDir)) { | 18 | if (!fs.existsSync(baseRequestDumpDir)) { |
19 | fs.mkdirSync(baseRequestDumpDir, { recursive: true }); | 19 | fs.mkdirSync(baseRequestDumpDir, { recursive: true }); |
20 | } | 20 | } |
21 | 21 | ||
22 | const doNotDump = !config.partner.dump_request; | 22 | const doNotDump = !config.partner.dump_request; |
23 | 23 | ||
24 | module.exports = async ( | 24 | module.exports = async ( |
25 | xid, task, endpointUrl, payload, axiosConfig, axiosResponse, exceptionOnHit, | 25 | xid, task, endpointUrl, payload, axiosConfig, axiosResponse, exceptionOnHit, |
26 | ) => { | 26 | ) => { |
27 | if (doNotDump) return; | 27 | if (doNotDump) return; |
28 | 28 | ||
29 | const { | 29 | const { |
30 | trx_id: trxId, | 30 | trx_id: trxId, |
31 | } = task; | 31 | } = task; |
32 | 32 | ||
33 | logger.verbose(`${MODULE_NAME} 826334AF: Dumping`, { | 33 | logger.verbose(`${MODULE_NAME} 826334AF: Dumping`, { |
34 | xid, | 34 | xid, |
35 | trxId, | 35 | trxId, |
36 | }); | 36 | }); |
37 | 37 | ||
38 | const dumpDir = path.join( | 38 | const dumpDir = path.join( |
39 | baseRequestDumpDir, | 39 | baseRequestDumpDir, |
40 | moment().format('YYYY-MM-DD'), | 40 | moment().format('YYYY-MM-DD'), |
41 | ); | 41 | ); |
42 | 42 | ||
43 | await mkdirIfNotExists(xid, dumpDir); | 43 | await mkdirIfNotExists(xid, dumpDir); |
44 | 44 | ||
45 | const dumpFileName = path.join( | 45 | const dumpFileName = path.join( |
46 | dumpDir, | 46 | dumpDir, |
47 | `trx_${trxId}_${xid}.txt`, | 47 | `trx_${trxId}_${xid}.txt`, |
48 | ); | 48 | ); |
49 | 49 | ||
50 | const data = `-------- BEGIN OF REQUEST DUMP -------- | 50 | const data = `-------- BEGIN OF REQUEST DUMP -------- |
51 | 51 | ||
52 | ${moment().format('YYYY-MM-DD HH:mm:ss')} | 52 | ${moment().format('YYYY-MM-DD HH:mm:ss')} |
53 | 53 | ||
54 | XID: ${xid} | 54 | XID: ${xid} |
55 | TRX ID: ${trxId} | 55 | TRX ID: ${trxId} |
56 | TRX DATE: ${task.created} | 56 | TRX DATE: ${task.created} |
57 | DESTINATION: ${task.destination} | 57 | DESTINATION: ${task.destination} |
58 | LOCAL PRODUCT: ${task.product} | 58 | LOCAL PRODUCT: ${task.product} |
59 | REMOTE PRODUCT: ${task.remote_product} | 59 | REMOTE PRODUCT: ${task.remote_product} |
60 | 60 | ||
61 | HTTP METHOD: POST | 61 | HTTP METHOD: POST |
62 | ENDPOINT URL: ${endpointUrl} | 62 | ENDPOINT URL: ${endpointUrl} |
63 | 63 | ||
64 | HIT CONFIG: ${(axiosConfig && JSON.stringify(axiosConfig, null, 2)) || '-'} | 64 | HIT CONFIG: ${(axiosConfig && JSON.stringify(axiosConfig, null, 2)) || '-'} |
65 | 65 | ||
66 | PAYLOAD: | 66 | PAYLOAD: |
67 | ${payload} | 67 | ${payload} |
68 | 68 | ||
69 | RESPONSE HEADERS: | 69 | RESPONSE HEADERS: |
70 | ${axiosResponse && axiosResponse.headers && JSON.stringify(axiosResponse.headers)} | 70 | ${axiosResponse && axiosResponse.headers && JSON.stringify(axiosResponse.headers)} |
71 | 71 | ||
72 | HTTP STATUS: ${axiosResponse && axiosResponse.status} ${(axiosResponse && axiosResponse.statusText) || ''} | 72 | HTTP STATUS: ${axiosResponse && axiosResponse.status} ${(axiosResponse && axiosResponse.statusText) || ''} |
73 | HIT EXCEPTION: ${ | 73 | HIT EXCEPTION: ${ |
74 | exceptionOnHit | 74 | exceptionOnHit |
75 | ? JSON.stringify({ code: exceptionOnHit.code, message: exceptionOnHit.message }, null, 2) | 75 | ? JSON.stringify({ code: exceptionOnHit.code, message: exceptionOnHit.message }, null, 2) |
76 | : '-' | 76 | : '-' |
77 | } | 77 | } |
78 | 78 | ||
79 | TYPE OF RESPONSE BODY: ${typeof axiosResponse.data} | ||
79 | RESPONSE BODY: | 80 | RESPONSE BODY: |
80 | ${ | 81 | ${ |
81 | ( | 82 | ( |
82 | axiosResponse && axiosResponse.data | 83 | axiosResponse && axiosResponse.data |
83 | && ( | 84 | && ( |
84 | (typeof axiosResponse.data === 'string' && axiosResponse.data) | 85 | (typeof axiosResponse.data === 'string' && axiosResponse.data) |
85 | || JSON.stringify(axiosResponse.data, null, 2) | 86 | || JSON.stringify(axiosResponse.data, null, 2) |
86 | ) | 87 | ) |
87 | ) || '' | 88 | ) || '' |
88 | } | 89 | } |
89 | 90 | ||
90 | -------- END OF REQUEST DUMP -------- | 91 | -------- END OF REQUEST DUMP -------- |
91 | `; | 92 | `; |
92 | 93 | ||
93 | try { | 94 | try { |
94 | logger.verbose(`${MODULE_NAME} E915E0D8: Writing file`, { | 95 | logger.verbose(`${MODULE_NAME} E915E0D8: Writing file`, { |
95 | xid, | 96 | xid, |
96 | dumpFileName, | 97 | dumpFileName, |
97 | }); | 98 | }); |
98 | 99 | ||
99 | await fsPromise.writeFile(dumpFileName, data); | 100 | await fsPromise.writeFile(dumpFileName, data); |
100 | 101 | ||
101 | logger.verbose(`${MODULE_NAME} 7275A26C: Writing last dump file`, { xid }); | 102 | logger.verbose(`${MODULE_NAME} 7275A26C: Writing last dump file`, { xid }); |
102 | await fsPromise.writeFile( | 103 | await fsPromise.writeFile( |
103 | path.join(baseDumpDir, 'last-request.txt'), | 104 | path.join(baseDumpDir, 'last-request.txt'), |
104 | data, | 105 | data, |
105 | ); | 106 | ); |
106 | } catch (e) { | 107 | } catch (e) { |
107 | logger.warn(`${MODULE_NAME} 29212E93: Exception on writing dump file`, { | 108 | logger.warn(`${MODULE_NAME} 29212E93: Exception on writing dump file`, { |
108 | xid, | 109 | xid, |
109 | eCode: e.code, | 110 | eCode: e.code, |
110 | eMessage: e.message, | 111 | eMessage: e.message, |
111 | }); | 112 | }); |
112 | } | 113 | } |
113 | }; | 114 | }; |
114 | 115 |
lib/hit.js
1 | const MODULE_NAME = 'HIT'; | 1 | const MODULE_NAME = 'HIT'; |
2 | 2 | ||
3 | const axios = require('axios').default; | 3 | const axios = require('axios').default; |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | const composePayload = require('./generic-xmlrpc/compose-payload'); | 8 | const composePayload = require('./generic-xmlrpc/compose-payload'); |
9 | const report = require('./report'); | 9 | const report = require('./report'); |
10 | const dumper = require('./dumper/request'); | 10 | const dumper = require('./dumper/request'); |
11 | const axiosSafeFailed = require('./axios-safe-failed'); | 11 | const axiosSafeFailed = require('./axios-safe-failed'); |
12 | const parseResult = require('./parse-result'); | 12 | const parseResult = require('./parse-result'); |
13 | 13 | ||
14 | const defaultAxiosConfig = { | 14 | const defaultAxiosConfig = { |
15 | headers: { | 15 | headers: { |
16 | 'User-Agent': 'Komodo-GW-ST24B', | 16 | 'User-Agent': 'Komodo-GW-ST24B', |
17 | 'Content-Type': 'text/xml', | 17 | 'Content-Type': 'text/xml', |
18 | }, | 18 | }, |
19 | timeout: config.partner.hit_timeout = 2 * 60 * 120, | 19 | timeout: config.partner.hit_timeout = 2 * 60 * 120, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | module.exports = async (xid, task) => { | 22 | module.exports = async (xid, task) => { |
23 | const methodName = config.partner.topuprequest_method_name || 'topUpRequest'; | 23 | const methodName = config.partner.topuprequest_method_name || 'topUpRequest'; |
24 | 24 | ||
25 | logger.verbose(`${MODULE_NAME} E4F52474: Processing task`, { | 25 | logger.verbose(`${MODULE_NAME} E4F52474: Processing task`, { |
26 | xid, methodName, task, | 26 | xid, methodName, task, |
27 | }); | 27 | }); |
28 | 28 | ||
29 | const params = { | 29 | const params = { |
30 | MSISDN: config.partner.msisdn, | 30 | MSISDN: config.partner.msisdn, |
31 | REQUESTID: task.trx_id, | 31 | REQUESTID: task.trx_id, |
32 | PIN: config.partner.pin, | 32 | PIN: config.partner.pin, |
33 | NOHP: task.destination, | 33 | NOHP: task.destination, |
34 | NOM: task.remote_product, | 34 | NOM: task.remote_product, |
35 | }; | 35 | }; |
36 | 36 | ||
37 | const payload = composePayload(methodName, params); | 37 | const payload = composePayload(methodName, params); |
38 | const axiosConfig = JSON.parse(JSON.stringify(defaultAxiosConfig)); | 38 | const axiosConfig = JSON.parse(JSON.stringify(defaultAxiosConfig)); |
39 | axiosConfig.headers['Content-length'] = payload.length; | 39 | axiosConfig.headers['Content-length'] = payload.length; |
40 | 40 | ||
41 | let response; | 41 | let response; |
42 | let eToDump; | 42 | let eToDump; |
43 | 43 | ||
44 | const endpointUrl = config.partner.url; | 44 | const endpointUrl = config.partner.url; |
45 | 45 | ||
46 | try { | 46 | try { |
47 | logger.verbose(`${MODULE_NAME} 47FDCA85: Going to HIT partner`, { | 47 | logger.verbose(`${MODULE_NAME} 47FDCA85: Going to HIT partner`, { |
48 | xid, | 48 | xid, |
49 | endpointUrl, | 49 | endpointUrl, |
50 | methodName, | 50 | methodName, |
51 | params, | 51 | params, |
52 | }); | 52 | }); |
53 | 53 | ||
54 | response = await axios.post(endpointUrl, params, axiosConfig); | 54 | response = await axios.post(endpointUrl, params, axiosConfig); |
55 | if (!response) { | 55 | if (!response) { |
56 | const e = new Error(`${MODULE_NAME} BAACC918: Empty response`); | 56 | const e = new Error(`${MODULE_NAME} BAACC918: Empty response`); |
57 | e.rc = '90'; | 57 | e.rc = '90'; |
58 | logger.warn(e.message, { xid }); | 58 | logger.warn(e.message, { xid }); |
59 | throw e; | 59 | throw e; |
60 | } | 60 | } |
61 | 61 | ||
62 | if (!response.data) { | 62 | if (!response.data) { |
63 | const e = new Error(`${MODULE_NAME} C816D842: Empty response data`); | 63 | const e = new Error(`${MODULE_NAME} C816D842: Empty response data`); |
64 | e.rc = '90'; | 64 | e.rc = '90'; |
65 | logger.warn(e.message, { xid }); | 65 | logger.warn(e.message, { xid }); |
66 | throw e; | 66 | throw e; |
67 | } | 67 | } |
68 | 68 | ||
69 | if (typeof response.data !== 'string') { | 69 | if (typeof response.data !== 'string') { |
70 | const e = new Error(`${MODULE_NAME} 32A75E8E: Response data is not a string`); | 70 | const e = new Error(`${MODULE_NAME} 32A75E8E: Response data is not a string`); |
71 | e.rc = '90'; | 71 | e.rc = '90'; |
72 | logger.warn(e.message, { xid }); | 72 | logger.warn(e.message, { xid }); |
73 | throw e; | 73 | throw e; |
74 | } | 74 | } |
75 | 75 | ||
76 | logger.verbose(`${MODULE_NAME} D0CBD82A: Parsing response`, { xid }); | 76 | logger.verbose(`${MODULE_NAME} D0CBD82A: Parsing response`, { xid }); |
77 | parseResult(xid, task.trx_id, response.data, false); | 77 | parseResult(xid, task.trx_id, response.data, false); |
78 | } catch (e) { | 78 | } catch (e) { |
79 | eToDump = e; | 79 | eToDump = e; |
80 | 80 | ||
81 | logger.warn(`${MODULE_NAME} 2653B932: Got an exception`, { | 81 | logger.warn(`${MODULE_NAME} 2653B932: Got an exception`, { |
82 | xid, | 82 | xid, |
83 | eCode: e.code, | 83 | eCode: e.code, |
84 | eMessage: e.message, | 84 | eMessage: e.message, |
85 | eRc: e.rc, | 85 | eRc: e.rc, |
86 | responseStatus: e.response && e.response.status, | 86 | responseStatus: e.response && e.response.status, |
87 | responseStatusText: e.response && e.response.statusText, | 87 | responseStatusText: e.response && e.response.statusText, |
88 | responseContentType: e.response && e.response.headers && e.response.headers['content-type'], | 88 | responseContentType: e.response && e.response.headers && e.response.headers['content-type'], |
89 | responseBody: e.response && e.response.data, | 89 | responseBody: e.response && e.response.data, |
90 | }); | 90 | }); |
91 | 91 | ||
92 | if (e.response) response = e.response; | 92 | if (e.response) response = e.response; |
93 | 93 | ||
94 | const rc = e.rc | 94 | const rc = e.rc |
95 | || (axiosSafeFailed(e) || '91') | 95 | || (axiosSafeFailed(e) || '91') |
96 | || '68'; | 96 | || '68'; |
97 | 97 | ||
98 | report(xid, { | 98 | report(xid, { |
99 | trx_id: task.trx_id, | 99 | trx_id: task.trx_id, |
100 | rc, | 100 | rc, |
101 | message: { | 101 | message: { |
102 | xid, | 102 | xid, |
103 | 'GW-ERROR': { | 103 | 'GW-ERROR': { |
104 | code: e.code, | 104 | code: e.code, |
105 | message: e.message, | 105 | message: e.message, |
106 | endpointUrl, | 106 | endpointUrl, |
107 | httpStatus: (e.response && e.response.status) || null, | 107 | httpStatus: (e.response && e.response.status) || null, |
108 | httpStatusText: (e.response && e.response.statusText) || null, | 108 | httpStatusText: (e.response && e.response.statusText) || null, |
109 | responseBody: e.response && e.response.data, | 109 | responseBody: e.response && e.response.data, |
110 | }, | 110 | }, |
111 | }, | 111 | }, |
112 | }); | 112 | }); |
113 | } finally { | 113 | } finally { |
114 | dumper( | 114 | dumper( |
115 | xid, | 115 | xid, |
116 | task, | 116 | task, |
117 | endpointUrl, | 117 | endpointUrl, |
118 | payload, | 118 | payload, |
119 | axiosConfig, | ||
120 | response.config || axiosConfig, | 119 | response.config || axiosConfig, |
120 | response, | ||
121 | eToDump, | 121 | eToDump, |
122 | ); | 122 | ); |
123 | } | 123 | } |
124 | }; | 124 | }; |