How to write custom errors and return messages with error keyword Solidity - error-handling

I tried to make a custom error but it doesn't return a message to revert
How could I make it return a message?
/// custom error
error MyCustomError(address _address);
if user { revert MyCustomError({address: msg.sender}) }
I got this error
Runtime error: revert

There are two errors:
1- if statement should be inside a function
2- when you defined the custom error,you defined the named parameter as _address
contract Test {
address public user;
/// custom error
error MyCustomError(address _address);
function revertError() public view {
// I just had to pass a valid condition. address(0)=0x0000000000000000000000000000000000000000
// if(user) would give this error: "Type address is not implicitly convertible to expected type bool"
if(user!=address(0))
{
revert MyCustomError({_address: msg.sender});
}
}
}

Related

#objc func expression resolves to an unused function

I'm new to swift and trying to call a method when the user presses some hotkey shortcut.
I have the following code: https://pastebin.com/pF8wk7jL
I'm trying to run togglePopover on this handler: hotKey.keyDownHandler.
This:
hotKey.keyDownHandler = {
self.togglePopover(_:)
}
returns the error "Expression resolves to an unused function"
and this:
hotKey.keyDownHandler = {
let call = self.togglePopover(_:)
call(_:)
}
returns the error "Cannot find call in scope".
Cannot fix the issue, can someone help?
If I do:
hotKey.keyDownHandler = {
print("teste")
}
works good!

Code analysis warning: CA1062 is really false positive?

In my code, I have a reference variable LogValidacionPagosDTO
public void InsertarArchivoXmlOk(ArchivoXmlDRO archivo, ref LogValidacionPagosDTO archivoRespuesta)
{
//Some code
}
When executing "code analysis" generates this warning
Warning CA1062
In externally visible method 'ArchivoXMLOperacion.ValidacionDuplicadosArchivoXmlFosyga(List<RegistroXmlFosygaDRO>, ref LogValidacionPagosDTO)',
validate local variable ''(*archivoRespuesta)'', which was reassigned from parameter 'archivoRespuesta', before using it.
Then try to validate the object as null
public void InsertarArchivoXmlOk(ArchivoXmlDRO archivo, ref LogValidacionPagosDTO archivoRespuesta)
{
if (archivoRespuesta == null || archivoRespuesta.DetalleRegistros == null)
throw new ExcepcionOperacion(HelperMensaje.Obtener(HelperCodigoMensaje.GEN_0003),
(int)CodigosHTTP.Error, archivoRespuesta, null);
//Some code
}
But this didn't solve the warning. I found this possible solution in Microsoft forum https://social.msdn.microsoft.com/Forums/en-US/fdb00899-c7ea-4e8e-b5f6-9768c2ac0001/ca1062-false-positive-in-externally-visible-method-xxx-validate-local-variable-x-which-was?forum=vstscode
But, I really need to know if this is a false positive, thks!

Syntax error on token "boolean", # expected

I want to check whether an alert message is present. For that i tried the code,
public boolean IsAlertPresent()
{
try
{
driver.switchTo().alert();
return true;
}
catch (NoAlertPresentException Ex)
{
return false;
}
}
But, error messages are shown in boolean and IsAlertPresent(). Boolean shows a message 'Syntax error on token "boolean", # expected' and IsAlertPresent() shows a message 'IsAlertPresent cannot be resolved to a type'.
I believe you have defined the method IsAlertPresent() inside another method. This is not allowed in Java. Define the method separately any your error will go away.
If you are writing the IsAlertPresent method in a jsp file, declare it in the <%! section rather than the <% section.
That way the method is part of the jsp class

Simple-LInked Error

I have placed the simple linkedin class on my server and added my api keys etc, however when i call the demo page i get the following error:
Parse error: syntax error, unexpected T_FUNCTION in /home/mycv/public_html/dev/linkedin_3.2.0.class.php on line 259
this is the code for the area around line: 259
if(is_array($http_code_required)) {
array_walk($http_code_required, function($value, $key) {
if(!is_int($value)) {
throw new LinkedInException('LinkedIn->checkResponse(): $http_code_required must be an integer or an array of integer values');
}
line 259: seems to refer to the second line starting with array walk.
Thanks
Anonymous functions only became available in PHP 5.3.0. Line 259 above uses an anonymous function, so that would explain the error if your version pre-dates support.
Just make the anonymous function as a named function and call it in the checkResponse function:
function **innerfunction**($value, $key) {
if(!is_int($value)) {
throw new LinkedInException('LinkedIn->checkResponse(): $http_code_required must be an integer or an array of integer values');
}
}
private function checkResponse($http_code_required, $response) {
// check passed data
if(is_array($http_code_required)) {
array_walk($http_code_required, **innerfunction**($value, $key));
}
}

Unhelpful error message in g++

I get these errors messages all over the place for g++ 4.1.2, and it doesn't help at all:
<file>:<line>: error: expected primary-expression before 'int'
<file>:<line>: error: expected ';' before 'int'
<file>:<line>: error: invalid use of qualified-name '::SuccessCode'
The piece of code where it happens is as simple as this:
class Test
{
static Status debug_function(void)
{
return Status::SuccessCode(); // this would be <file>:<line> mentioned above
// and this one too:
// return Status::FailureCode("test");
}
};
And here's the code for Status (and yes it's properly included, because I would have an error if the include file was invalid):
namespace CODES
{
enum Values { Success = 0, Failed = 1 };
}
class Status
{
private:
CODES::Values code;
string msg;
public:
Status(CODES::Values val, const string &i_msg ): code(val), msg(i_msg) {}
static Status SuccessCode(void)
{
return Status(CODES::Success, "");
}
static Status FailureCode(const string &fail_msg)
{
return Status(CODES::Failed, fail_msg);
}
};
So, what is wrong with this piece of code ? And it compiles properly under VC++!
EDIT: Actually, the code for SuccessCode and FailureCode are in a *.cpp file. I put them in the class declaration because the error message is still the same!
Without a complete program, I'll have to guess. My guess is: you have a #define Status int somewhere in your program.