I created a like button in my solid-js app that does not allow someone to like a post they authored. For this, I wrote the following code:
const isPostAuthor = postAuthor.authorId === currentUser.id;
if (isPostAuthor) {
return next({
status: 401,
message: "You can`t add a like on your own post"
});
In order to check if the user who likes is the author of the post and to return an error message with status 401 to the user if it is the case. It works fine only that the error message is not displayed to the user instead, it instead displays a red cross to the user; However, this message is displayed on the command line. I thought it was a status error, so I changed the error status 401 to status 400 and then 404 but it still didn't show the error message to the user . So I tried out of curiosity the status 200 used for successful operations, it did return a message to the user but in green color yet the operation failed, I don't know why it works with the success code 200 but not with error codes . Could someone help me?
If my hypothesis is correct based on your explanation, wherever you are receiving this response, you need to add two sections to catch this response, one is success response and the other is error response, because as you said for 200 status code, it is showing the message and indicates the success response, that is the reason you could able to see the message for the user.
You can try adding error section in your code wherever you are receiving this response and see if you could able to catch this user message
hope my hypothesis helps :)
Related
I'm trying to make a discord bot and it works fine but there's one thing that doesn't work. For some reason, it gives me this error when I try to do something with the bot. UnhandledPromiseRejectionWarning: ReferenceError: message is not defined I have no idea why. Heres the code that has the error: const collector = reactionMessage.createReactionCollector((reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("MANAGE_NICKNAMES"), { dispose: true }
Heres's some context of what the bot is if that helps: It's a ticket bot, where members can report problems to mods in a private channel. It creates a channel, and sends a message saying "staff will be with you shortly" and puts reactions so that you can close the ticket. When I try to close the ticket, it gives the error. The error happens around user.id).haspermission("MANAGE_NICKNAMES") That code is supposed to make people without the MANAGE_NICKNAMES permission unable to use the reactions, and theres no place in the code that i'm supposed to put 'message' so why is this happening?
Try replacing places message to reaction.message and if it doesn't work try using partials
I am using strongloop 4 (lb4). I am facing one issue that in error object I need to one more custom parameter in the error object.
I want it on the global level. On every error, I want to add that custom parameter in every error message.
In loopback4 global error handling is done by src/sequence.ts.
Suppose the error message object is.
{
"error": {
"statusCode": 400,
"name": "xyz",
"message": "firstName is required"
}
}
I want error object output like.
{
"error": {
"customParam" : "customParam",
"statusCode": 400,
"name": "xyz",
"message": "firstName is required"
}
}
Cross-posting the answer I gave on GitHub in https://github.com/strongloop/loopback-next/issues/1867#issuecomment-434247807
Building HTTP error responses is a tricky business. It's easy to get it wrong and open your application to attacks.
In LoopBack (both 3.x and 4.x), we use our strong-error-handler middleware to take care of this. See Handling Errors in our docs.
Here are the important security constraints to keep in mind:
In production mode, strong-error-handler omits details from error responses to prevent leaking sensitive information:
For 5xx errors, the output contains only the status code and the status name from the HTTP specification.
For 4xx errors, the output contains the full error message (error.message) and the contents of the details property (error.details) that ValidationError typically uses to provide machine-readable details about validation problems. It also includes error.code to allow a machine-readable error code to be passed through which could be used, for example, for translation.
In debug mode, strong-error-handler returns full error stack traces and internal details of any error objects to the client in the HTTP responses.
Now that I have warned you, LoopBack 4 makes it very easy to format the error messages your way. Just provide a custom implementation of the Sequence action reject. See Customizing Sequence Actions in our docs, it explain how to create a custom send action. The solution for reject is pretty much the same, you just need a different signature for the action function.
export class CustomRejectProvider implements Provider<Reject> {
// ...
action({request, response}: HandlerContext, error: Error) {
// handle the error and send back the error response
// "response" is an Express Response object
}
}
Caveat: some errors thrown by LB4 have only code set, these errors need a bit of pre-processing to decide what HTTP status code they should trigger. (For example, the error code ENTITY_NOT_FOUND should be mapped to the status code 404). The built-in reject action does not yet expose this pre-processing for consumption by custom reject actions. It's an oversight on our side, l created a new issue https://github.com/strongloop/loopback-next/issues/1942 to keep track of that.
I tried adding error object into new object.
let error = new Error();
error.name = 'Invalid_OTP_AttemptsError';
error.status = 422;
error.message = 'You’ve exceeded the maximum number of One-Time Password (OTP) attempts';
let data={...error};
data.retryCount=foundMb.retryCount
data.resendCount=foundMb.resendCount
return callback(null,data);
I want to clear all pending_update_count in my bot!
The output of below command :
https://api.telegram.org/botxxxxxxxxxxxxxxxx/getWebhookInfo
Obviously I replaced the real API token with xxx
is this :
{
"ok":true,"result":
{
"url":"",
"has_custom_certificate":false,
"pending_update_count":5154
}
}
As you can see, I have 5154 unread updates til now!! ( I'm pretty sure this pending updates are errors! Because no one uses this Bot! It's just a test Bot)
By the way, this pending_update_count number are increasing so fast!
Now that I'm writing this post the number increased 51 and reached to 5205 !
I just want to clear this pending updates.
I'm pretty sure this Bot have been stuck in an infinite loop!
Is there any way to get rid of it?
P.S:
I also cleared the webhook url. But nothing changed!
UPDATE:
The output of getWebhookInfo is this :
{
"ok":true,
"result":{
"url":"https://somewhere.com/telegram/webhook",
"has_custom_certificate":false,
"pending_update_count":23,
"last_error_date":1482910173,
"last_error_message":"Wrong response from the webhook: 500 Internal Server Error",
"max_connections":40
}
}
Why I get Wrong response from the webhook: 500 Internal Server Error ?
I think you have two options:
set webhook that do nothing, just say 200 OK to telegram's servers. Telegram wiil send all updates to this url and the queque will be cleared.
disable webhook and after it get updates by using getUpdates method, after it, turn on webhook again
Update:
Problem with webhook on your side. You can try to emulate telegram's POST query on your URL.
It can be something like this:
{"message_id":1,"from":{"id":1,"first_name":"FirstName","last_name":"LastName","username":"username"},"chat":{"id":1,"first_name":"FirstName","last_name":"LastName","username":"username","type":"private"},"date":1460957457,"text":"test message"}
You can send this text as a POST query body with PostMan for example, and after it try to debug your backend.
For anyone looking at this in 2020 and beyond, the Telegram API now supports clearing the pending messages via a drop_pending_updates parameter in both setWebhook and deleteWebhook, as per the API documentation.
Just add return 1; at the end of your hook method.
Update:
Commonly this happens because of queries delay with the database.
I solved is like this
POST tg.api/bottoken/setWebhook to emtpy "url"
POST tg.api/bottoken/getUpdates
POST tg.api/bottoken/getUpdates with "offset" last update_id appeared before
doing this serveral times
POST tg.api/bottoken/getWebhookInfo
had a look if all away.
POST tg.api/bottoken/setWebhook with filled "url"
If you are using webhook, you can follow these steps
On your web browser, enter the following url with your right value of bot
https://api.telegram.org/bot/getWebhookInf
You will get a result like this on your screen
{"ok":true,"result":{"url":"url_value",...}}
On the displayed result, copy the entire url_value without quotes and replace it on this second url
https://api.telegram.org/bot/setWebhook?url=url_value&drop_pending_updates=True
Enter the second url with right bot and url_value in your web browser then press ENTER
Done!
i solve it by Change file access permissions file - set permissions file to 755
and second increase memory limit in php.ini file
A quick&dirty way is to get a temporary webhook here: https://webhook.site/ and
set your webhook to that (it will answer with a HTTP/200 code everytime, reseting your pending messages to zero)
I faced the same issue for my tele bot after user edited existing message. My bot receives update with editedMessage continuously, but update.hasMessage() was empty. As a result number of updates rocketly increased and my bot stack.
I solved this issue by adding handling for use case when message is missing - send 200 code:
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
update = MAPPER.readValue(event.getBody(), Update.class);
if (!update.hasMessage()) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200) // -> !!!!!! return code 200
.withBody("message is missing")
.withIsBase64Encoded(false);
}
... ... ...
I'm using node-steamcommunity to get user's inventory
The module responded "[Error: malformed response]"
After doing some debugging I've managed to see the raw response from steamcommunity server (request to: https://steamcommunity.com/profiles/STEAM_ID/inventory/json/570/1/):
The response is
{"success": false}
The problem is that the normal response should contain the error like this:
{"success": false, "Error": "The profile is private"}
So now I can neither get the user's inventory nor the reason why I can't do this.
Could some one point me to the right way of getting the user's inventory from steam
Update:
after doing some investigation I've found something interesting:
When I login as a normal user, i get a sessionid.
With that key I can access to profile inventory
When I start bot I get also get sessionId but when I'm trying to request with bot's session id I get the { success: false} response without any explanation.
Does anyone has an idea what is wrong?
1 is not a correct contextid for Dota 2 (570).
You're looking for https://steamcommunity.com/profiles/STEAM_ID/inventory/json/570/2/ (notice the 2 at the end instead of 1).
You tried accepting trade? If yes then maybe need use:
let key = SteamTotp.getConfirmationKey(identity_secret, time, 'allow');
key is argument for response function
I have problems when I use bottlenose.
According to its instructions, I need to add a error_handler as per instructions.
in the instructions I placed the function:
def error_handler(err):
ex = err['exception']
if isinstance(ex, HTTPError) and ex.code == 404:
time.sleep(random.expovariate(0.1))
return True
The examples in the instruction says to use this line:
amazon = bottlenose.Amazon(ErrorHandler=error_handler)
I have this:
amazon = bottlenose.Amazon(AWSAccessKeyId=ACCESS_KEY_ID, AWSSecretAccessKey = SECRET_KEY,AssociateTag = ASSOC_TAG)
But I'm getting no correct response. Why?
Are you submitting requests too quickly? You need to slow down. One request per second is a good speed.
The Amazon Product Advertising API returns errors in three categories so that you can easily determine how best to handle the problem:
2XX errors are caused by mistakes in the request. For example, your
request might be missing a required parameter. The error message in
the response gives a clear indication what is wrong.
4XX errors are non-transient errors. Upon receiving this error,
resubmit the request.
5XX errors are transient errors reflecting an error internal to
Amazon. A 503 error means that you are submitting requests too
quickly and your requests are being throttled. If this is the case,
you need to slow your request rate to one request per second.