GithubHelp home page GithubHelp logo

ied206 / betterwin32errors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkropat/betterwin32errors

0.0 2.0 0.0 10 KB

A better interface to the constants defined in winerror.h

License: MIT License

C# 51.48% PowerShell 46.03% Batchfile 2.49%

betterwin32errors's Introduction

BetterWin32Errors

A better interface to the constants defined in winerror.h

Simplified Error Handling

Instead of:

if (!SomeWin32ApiCall(...))
{
    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}

You can do this:

if (!SomeWin32ApiCall(...))
{
    throw new BetterWin32Errors.Win32Exception();
}

Access To Error Constants

Instead of re-defining platform constants in your code:

const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_PATH_NOT_FOUND = 3;

if (!SomeWin32ApiCall(...))
{
    var error = Marshal.GetLastWin32Error();
    if (error == ERROR_FILE_NOT_FOUND)
    {
        // ...do something...
    }
    else if (error == ERROR_PATH_NOT_FOUND)
    {
        // ...do something else...
    }
    else
    {
      throw new System.ComponentModel.Win32Exception(error);
    }
}

You can do this:

if (!SomeWin32ApiCall(...))
{
    var error = Win32Exception.GetLastWin32Error();
    if (error == Win32Error.ERROR_FILE_NOT_FOUND)
    {
        // ...do something...
    }
    else if (error == Win32Error.ERROR_PATH_NOT_FOUND)
    {
        // ...do something else...
    }
    else
    {
        throw new Win32Exception(error);
    }
}

Exception Has Both ID and Message

try
{
    // ...some code...
}
catch (BetterWin32Errors.Win32Exception ex)
	when (ex.Error == Win32Error.ERROR_FILE_NOT_FOUND) // use `Error` to get the error ID
{
	Console.WriteLine("Warning: " + ex.ErrorMessage);
    // Output: "Warning: The system cannot find the file specified"
}

Installation

Install-Package BetterWin32Errors

The only import you need to know is:

using BetterWin32Errors;

Limitations

There are thousands of error constants defined in <winerror.h>. All error constants included in this library were parsed using a script. As such, there is no guarantee that the error constants have the correct values in all cases and for all platforms. Feel free to submit an issue if you run into such a problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.