Getting invalid version number error when connecting to peer - ipfs

I'm getting an error when trying to connect to a node. The IPFS instance is created in a class, with a JavaScript factory function. The connect function looks like this:
async connectToPeer(multiaddr, protocol = "/p2p-circuit/ipfs/") {
try {
console.log("swarm.connect: ", protocol + multiaddr);
await this.node.swarm.connect(protocol + multiaddr);
} catch (e) {
throw(e);
}
}
This will be passed to swarm.connet: /p2p-circuit/ipfs/QmWxWkrCcgNBG2uf1HSVAwb9RzcSYYC2d6CRsfJcqrz2FX
This is the error I get:
(node:24449) UnhandledPromiseRejectionWarning: AggregateError:
Error: Invalid version, must be a number equal to 1 or 0
at Function.validateCID (/home/user/orbit-playground/music-app-no-react/node_modules/cids/src/index.js:312:13)
at new CID (/home/user/orbit-playground/music-app-no-react/node_modules/cids/src/index.js:151:9)
at new CID (/home/user/orbit-playground/music-app-no-react/node_modules/class-is/index.js:15:17)
at Function.exports.createFromCID (/home/user/orbit-playground/music-app-no-react/node_modules/peer-id/src/index.js:248:32)
at ClassIsWrapper.dial (/home/user/orbit-playground/music-app-no-react/node_modules/libp2p/src/circuit/index.js:107:30)
at TransportManager.dial (/home/user/orbit-playground/music-app-no-react/node_modules/libp2p/src/transport-manager.js:89:30)
at DialRequest.dialAction (/home/user/orbit-playground/music-app-no-react/node_modules/libp2p/src/dialer/index.js:149:36)
at pAny.addrs.map (/home/user/orbit-playground/music-app-no-react/node_modules/libp2p/src/dialer/dial-request.js:58:29)
at maybeSettle (/home/user/orbit-playground/music-app-no-react/node_modules/p-some/index.js:31:11)
at /home/user/orbit-playground/music-app-no-react/node_modules/p-some/index.js:69:23
It is able to connect to peers, if I list connected peers the list is not empty.

Related

Caught exception: TypeError: listener is not a function while publish redis client using node v16.16.0, redis-cli 7.0.0 & "redis": "^4.2.0",

When using node v16.16.0, redis-cli 7.0.0 & "redis": "^4.2.0"
getting such exeption below:
Caught exception: TypeError: listener is not a function Exception
origin: uncaughtException[2022-07-18T07:39:30.386Z] process.on
uncaughtException ERRORCODE 105199 TypeError: listener is not a
function
at Function._RedisCommandsQueue_emitPubSubMessage (/mnt/c/Projects/konnectcore/app/sse/sse/node_modules/#redis/client/dist/lib/client/commands-queue.js:241:9)
at RedisCommandsQueue._RedisCommandsQueue_handlePubSubReply (/mnt/c/Projects/konnectcore/app/sse/sse/node_modules/#redis/client/dist/lib/client/commands-queue.js:283:100)
It's working fine while using node redis "redis": "^2.8.0".
I had a similar issue where I was using probably some old way of subscribing and receiving the messages. Something like this:
sub.on('message', (channel, message) => {
redisClient.hSet('values', message, someFunction);
});
sub.subscribe('channel');
I hope you are using the right way of publishing and subscribing to a channel in redis client. Here is one example from their documentation:
// This is how you create the client
import { createClient } from 'redis';
const client = createClient();
// This is the subscriber part
const subscriber = client.duplicate();
await subscriber.connect();
await subscriber.subscribe('channel', (message) => {
console.log(message); // 'message'
});
// This is an example of how to publish a message to the same channel
await publisher.publish('channel', 'message');
Here is the link if you would like to see some more details about publishing and subscribing of the messages using node-redis client: https://github.com/redis/node-redis#pubsub

Is there a way to have Nest use the ExceptionsHandler error message as the API response message when an exception is thrown?

When an error is thrown within my Nest API, often times the error that is thrown is not an HttpException, and therefore the standard Nest API response that I see from the client's side is:
{
"statusCode": 500,
"message": "Internal server error"
}
But when I look at the console log, there is a much more descriptive error message, for example:
[Nest] 18476 - 06/23/2022, 2:28:07 PM ERROR [ExceptionsHandler] Cannot perform update query
because update values are not defined.
UpdateValuesMissingError: Cannot perform update query because update values are not defined.
Is there a way I can route this message to be the in the response body of the response given back to the API client?
You can use Global Filter in NestJs to catch error and then throw a good error.
For exemple, you can create a Exception Filter
// UpdateValuesMissingError.exception-filter.ts
import { ArgumentsHost, Catch, ExceptionFilter } from '#nestjs/common';
import { Request, Response } from 'express';
#Catch(UpdateValuesMissingError)
export class UpdateValuesMissingErrorFilter implements ExceptionFilter {
catch(exception: UpdateValuesMissingError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
console.log(exception);
response.status(400).json({
statusCode: 400,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
and in your main.ts add this
app.useGlobalFilters(new HttpExceptionFilter());
And now in response.status(400).json({}) you can add everything you want in your body. Like the error message of Axios.

Node Lambda: MySQL query never runs

Testing with Postman, I'll try to make this as clear as possible, please advise if this is not making sense.
I have a Lambda that uses MySQL RDS database on AWS and works fine locally when accessing the database on AWS. After successfully getting a JWT from an auth endpoint I try to hit the login endpoint and I get a 502 Bad Gateway. Using the CloudWatch logs I can trace the failure to right before the login query runs. I've confirmed that my MySQL config is correct and that I have a connection to the database. The lambda and the database are in the same region DB: us-east-1f, lambda: us-east-1.
I've confirmed the OPTIONS and POST request methods for this endpoint both are set up with CORS enabled in the API Gateway. I'm using my serverless.yml to set cors: true on all the endpoints even though I'm using app.use(cors()) in my index file.
The error message for the 502 is, {"message": "Internal server error"}
Here is the point of failure in my code:
'use strict';
const mysql = require('./index');
module.exports = {
loginSql: async (email, password) => {
// MAKES IT HERE AND THE PARAMS ARE CORRECT
try {
console.log('IN TRY %%%%%%%%%%%%%%');
// SEEMS TO DIE HERE
const results = await mysql.query({
sql: `SELECT
id, first_name, last_name, email
FROM users
WHERE email = ?
AND password = ?`,
timeout: 50000,
values: [email, password],
});
// NEVER MAKES IT HERE /////////
console.log('QUERY RAN %%%%%%%%%%%%');
mysql.end();
if (results.length < 1) return false;
return results;
} catch (error) {
// DOESN'T THROW ERROR
console.log('LOGIN DB ERROR', error);
throw new Error('LOGIN DB ERROR THROWN', error);
}
},
};
I just created the exact same use case in that I have a LAMBDA function written in Java querying data from a MySQL RDS instance. It works perfectly.
Here is your issue:
To connect to the RDS instance from a Lambda function, you must set the inbound rules using the same security group as the RDS Instance. For details, How do I configure a Lambda function to connect to an RDS instance?.

Send multiple requests to Flask API

I ran into this exception while I was working with Angular to show my Flask API REST data which is deployed on an nginx server:
{ "error": "(_mysql_exceptions.ProgrammingError) (2014, \"Commands out of sync; you can't run this command now\") [SQL: 'SELECT ......" }
This exception is caused by this function that sends two requests at the same time to the database:
ngOnInit() {
let url = this.baseUrl + `/items/${this.id}`;
this.httpClient.get(url).subscribe((data : Array<any>)=> {
this.ItemToEdit = data;
});
let url = this.baseUrl + '/products'
this.httpClient.get(url);
}
I use SQLAlchemy in the API with Mysql database. I thought that If I add a pool connection it will be resolved but It didn't, I still get same exception:
engine = create_engine(connection_string, pool_size=20, max_overflow=0)
What exactly should I do to handle this ?
Is there anything else to set on the server side to make this function work without getting an exception ?
EDIT:
Is this an observable handling problem or it can be fixeb by using Gunicorn with nginx on server side ?
Using Gunicorn solved my problem.
I launch the application this way:
gunicorn -b 0.0.0.0:5000 --workers=5 myapi:app
And I don't see the error anymore even with 3 mysql requests in ngOnInit

Xamarin Social component fails to retrieve Twitter json stream

I'm using the following to read Twitter json. It works with one uri and not another. The uri's work with the Twitter API console but not Xamarin.Social. I have read and write permissions on the Twitter app so I can't see where I'm going wrong.
https://api.twitter.com/1.1/account/settings.json <-- works
https://api.twitter.com/1.1/users/show.json?screen_name=AUserName <-- fails (see error below)
request.GetResponseAsync ().ContinueWith (response => {
if (response.IsFaulted)
{
Console.WriteLine (response.Exception.Flatten ());
}
var json = response.Result.GetResponseText ();
System.AggregateException: One or more errors occured ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0030c] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1606
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1423
--- End of inner exception stack trace ---
--> (Inner exception 0) System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0030c] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1606
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1423
[quick google search gave this but not sure if its relevant: https://dev.twitter.com/discussions/15206]
// UPDATE ***********
Does this extra infor help or you need more details? If so then what details are required?
public Account Account
{
get
{
var task = Service.GetAccountsAsync ()
.ContinueWith (accounts =>
{
return accounts.Result.ToList ().FirstOrDefault ();
});
return task.Result;
}
set
{
AccountStore.Create ().Save (value, SocialPlatform.ToString ());
}
}
// later on
// when endpoint = "https://api.twitter.com/1.1/account/settings.json" <-- works, json returned
// when endpoint = "https://api.twitter.com/1.1/users/show.json?screen_name=XXXX" <-- IsFaulted with above error,
var request = Service.CreateRequest ("GET", endpoint, Account);
request.GetResponseAsync ().ContinueWith (response => {
if (response.IsFaulted)
{
Console.WriteLine (response.Exception.Flatten ());
return;
}
var json = response.Result.GetResponseText ();
Console.WriteLine (json);
});
It seems like you are not authorised when you make this call.
From Xamarin.Social documentation.
Xamarin.Social uses the Xamarin.Auth library to fetch and store
Account objects.
Each service exposes a GetAuthenticateUI method that returns a
Xamarin.Auth.Authenticator object that you can use to authenticate the
user. Doing so will automatically store the authenticated account so
that it can be used later.
The reason why it works in Twitter API console is that you have authorised there prior to making a call.
If you are already authorising in your app then please post the code you use to authorise.