question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responder cb is rejecting the callback data internally

See original GitHub issue

Hello,

This is my first time using Cote, I really liked it, awesome library for microservices. Great job! I don’t know if I’m doing something wrong, but I’ve doing some tests and also doing the same tests on the readme, but I always receive the response as an error (on the catch block).

Versions:

"cote": "^0.17.0",
"@types/cote": "^0.14.3"

Here is the example code:

import * as cote from 'cote';
import { WBA } from './Webbuffet';


export interface ServiceResponderCallback {
    (request: any, callback: (result: any) => void);
}

export class ServiceController {
    private static _instance: ServiceController;
    private discoveryOptions: cote.DiscoveryOptions;
    private res: cote.Responder;
    private sub: cote.Subscriber;

    private constructor() {
        const config = WBA.config();

        /*          default values
                    helloInterval: 2000,
                    checkInterval: 4000,
                    nodeTimeout: 5000,
                    masterTimeout: 6000,
                    monitor: false,
                    log: true,
                    helloLogsEnabled: true,
                    statusLogsEnabled: true,
                    ignoreProcess: false */
        this.discoveryOptions = {
            broadcast: '255.255.255.255',
            ignoreProcess: true,
            monitor: false,
            log: false

        };
        this.res = new cote.Responder({ name: `${config.name} Responder`, namespace: config.name }, this.discoveryOptions);
        this.sub = new cote.Subscriber({ name: `${config.name} Subscriber`, namespace: config.name }, this.discoveryOptions);
    }

    //singleton get instance
    public static get Instance(): ServiceController {
        return this._instance || (this._instance = new this());
    }


    public get $res(): cote.Responder {
        return this.res;
    }

    public async init(): Promise<void> {
        try {


        } catch (ex) {
            WBA.logError(new Error(ex));
        }
    }

    public publishMsg(namespace: string, topic: string, msg: any): WBA.Webbuffet {
        try {
            if (!namespace || !topic || !msg) return WBA.error(WBA.Status.ERROR_NULL_PARAMETER, 'namespace/topic/msg is NULL');

            const publisher = new cote.Publisher({ name: `${WBA.config().name} Publisher`, namespace: namespace }, this.discoveryOptions);
            publisher.publish(topic, msg);

            return WBA.success(true);
        } catch (ex) {
            return WBA.exception(new Error(ex));
        }
    }

    public async sendMsg(namespace: string, topic: string, msg: any, timeout?: number): Promise<WBA.Webbuffet> {
        try {
            if (!namespace || !topic || !msg) return WBA.error(WBA.Status.ERROR_NULL_PARAMETER, 'namespace/topic/msg is NULL');

            const requester = new cote.Requester({ name: `${WBA.config().name} Requester`, namespace: namespace }, this.discoveryOptions);
            const operationTimeout = (timeout) ? timeout : 15000;
            const promiseOperation = requester.send({ type: topic, val: msg });
            const response = await promiseOperation.timeout(operationTimeout);
            return WBA.success(response);

        } catch (ex) { //the response is being received here, instead of the response variable
            if (ex.name && ex.name === 'TimeoutError') return WBA.error(WBA.Status.PROMISE_TIMEOUT, new Error(ex).message);
            else return WBA.exception(new Error(ex));
        }
    }

    public registerResponder(topic: string, callback: ServiceResponderCallback): WBA.Webbuffet {
        try {
            if (!topic || !callback) return WBA.error(WBA.Status.ERROR_NULL_PARAMETER, 'topic/callback is NULL');

            this.res.on(topic, callback);
            return WBA.success(true);
        } catch (ex) {
            return WBA.exception(new Error(ex));
        }
    }
}

    //sync, request/response
    export function sendMsg(namespace: string, topic: string, msg: any, timeout?: number): Promise<Webbuffet> {
        return ServiceController.Instance.sendMsg(namespace, topic, msg, timeout);
    }

    export function registerResponder(topic: string, callback: ServiceResponderCallback): Webbuffet {
        return ServiceController.Instance.registerResponder(topic, callback);
    }

WBA is my framework, the config.name refers to the name of the microservice. I’m using 2 frameworks that use the WBA lib with cote as example, one of them is WBA-REPORT, another is WBA-CORE, I’m sending an request event to ‘WBA-REPORT’ from ‘WBA-CORE’.

WBA-CORE code: await WBA.sendMsg('WBA-REPORT', 'test', 'Cote is awesome!');

WBA-REPORT code:

//first code test
const coteController = ServiceController.Instance;
coteController.$res.on('test', (req, cb) => {
    cb('Hello from WBA-REPORT'); /this line here is being delivered as an error/promise rejected
});

//second code test:
function tocsonResponder(req: any, res: any): void {
    console.log(req);
    res('Hello from WBA-REPORT'); //this line here is being delivered as an error/promise rejected
}
WBA.registerResponder('test', tocsonResponder);

Please let me know if you need more info. Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dasherswcommented, Aug 2, 2018

Hello! You look like you are mixing up callbacks and promises. In that setting, the first parameter passed to the callback is taken as the error. You can either stop using callbacks altogether and just use promises, or you can add an error parameter in the front while calling a callback with the result.

0reactions
shussoncommented, Sep 19, 2018

Just ran into this too, I’d agree with adding some more promise examples to the README. It’d also be nice to see examples with await/async. I’ll add a PR if I get time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnhandledPromiseRejectionWar...
Hello,. I want to return promise instead of using callback as parameter : Requester side : import * as cote from 'cote'; const...
Read more >
datatables: how to catch error when using custom ajax function?
After trying a few different methods, I think your best bet will be to call the callback function with your data on a...
Read more >
[Libguestfs] [libnbd PATCH 3/8] pread: Reject server SR read ...
... PATCH 3/8] pread: Reject server SR read response with no data chunks ... But a callback > function will only be reached...
Read more >
User Callback - Libwebsockets
This callback is the way the user controls what is served. All the protocol detail is hidden and handled by the library. For...
Read more >
Refactoring Asynchrony in JavaScript
An example of an asynchronous callback before refactoring to promises – from KiwiIRC #581. (4) cb is splittable. 1 + function addTranslations (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found