interface.js --> connector.js --> server.js
const server = require('./server')
exports.makeRequest = function () {
// more code...
return server.response()
}
exports.response = function () {
// more code...
return { // Correct response
ok: true,
data: [1, 2, 3, 4, 5],
}
}
const connector = require('./connector')
const response = connector.makeRequest()
console.log('Response from server')
console.log(response)
console.log('---------------------------')
console.log('Proccess data: item + 1')
for (const data of response.data) {
console.log(data + 1)
}
Response from server
{ data: [ 1, 2, 3, 4, 5 ] }
---------------------------
Proccess data: item + 1
2
3
4
5
6
exports.response = function () {
return { // Incorrect response
ok: false,
message: 'The data is not available',
}
}
Response from server
{ ok: false, message: 'The data is not available' }
---------------------------
Proccess data> item + 1
/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/interface.js:10
for (const data of response.data) {
^
TypeError: response.data is not iterable
at Object.<anonymous> (/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/interface.js:10:29)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
at startup (internal/bootstrap/node.js:228:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)
const connector = require('./connector')
const response = connector.makeRequest()
console.log('Response from server')
console.log(response)
console.log('---------------------------')
console.log('Proccess data: item + 1')
if (response.data) {
for (const data of response.data) {
console.log(data + 1)
}
} else {
console.log(response.message)
}
const server = require('./server')
exports.makeRequest = function () {
const response = server.response()
count += response.data.length // To do something
return response
}
const server = require('./server')
exports.makeRequest = function () {
const response = server.response()
if (response.data) {
count += response.data.length // To do something
return response
} else {
return response
}
}
exports.response = function () {
return [1, 2, 3, 4, 5] // Direct response
}
const connector = require('./connector')
const response = connector.makeRequest()
console.log('Response from server')
console.log(response)
console.log('---------------------------')
console.log('Proccess data: item + 1')
for (const data of response) {
console.log(data + 1)
}
Response from server
{ data: [ 1, 2, 3, 4, 5 ] }
---------------------------
Proccess data> item + 1
2
3
4
5
6
exports.response = function () {
throw new Error('The data is not available')
}
/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/server.js:15
throw new Error('The data is not available')
^
Error: The data is not available
at Object.exports.response (/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/server.js:15:9)
at Object.exports.makeRequest (/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/connector.js:4:17)
at Object.<anonymous> (/home/renier/Development/DESARROLLO/APP_PRUEBAS/ERROR_TEST/interface.js:4:23)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
at startup (internal/bootstrap/node.js:228:19)
const connector = require('./connector')
const response = connector.makeRequest()
console.log('Response from server')
console.log(response)
console.log('---------------------------')
console.log('Proccess data: item + 1')
try {
for (const data of response.data) {
console.log(data + 1)
}
} catch(e) {
console.log(e.message)
}
const server = require('./server')
exports.makeRequest = function () {
const response = server.response()
count += response.data.length // To do something
return response
}
if (response.data) {
for (const data of response.data) {
console.log(data + 1)
}
} else {
console.log(response.message)
}
//-------------------------------------
try {
for (const data of response.data) {
console.log(data + 1)
}
} catch(e) {
console.log(e.message)
}
Utiliza el mismo mecanismo de error de http: Http Status
301 (Moved Permanently)
302 (Found)
400 (Bad Request)
401 (Unauthorized)
403 (Forbidden)
404 (Not Found)
405 (Method Not Allowed)
406 (Not Acceptable)
412 (Precondition Failed)
415 (Unsupported Media Type)
500 (Internal Server Error)
501 (Not Implemented)
http_status: 400
{
message: "The data is not available",
code: "data_not_available"
}
if (error.code == 'data_not_available') {
// ......
}
if (error.status == 410) { // data not available
// ......
}
http_status: 410
{
message: "The data is not available",
code: "data_not_available"
}
def get(self, request, *args, **kwargs):
ga_service = GoogleAnaltyticsService()
try:
chat_platform_service = ChatPlatformService(request.user.organization)
members = chat_platform_service.get_members()
return Response(dict(ok=True, members=members), status=status.HTTP_200_OK)
except Exception as e:
logger.error(str(e))
ga_service.track_event(
user_id=None,
category="Platform - Core - Exception",
action="Error catched on {}".format(request.user.organization.get_main_chat_platform_integration()),
label=str(e)
)
return Response(dict(ok=False, detail=str(e)), status=status.HTTP_200_OK)