• Handling WebAPI exceptions with Angular http interceptor

    Handling WebAPI exceptions with Angular http interceptor

    It’s good to return meaningful HTTP status codes upon server exceptions. Whether it be invalid model state, conflict upon trying to create/update an object in the database, unauthorized access or something else it adds additional value and sends a message – one that’s different from generic 400’s or 500’s. These messages can then be used to take certain kinds of actions on the client side based on what happened.

    For example you could register a custom ApplicationExceptionHandler.cs inside your WebApiConfig.cs which would then be able to catch and handle any kind of exception thrown by lower server layers (such as business layer or the DB/ORM layer):

    You need to register ApplicationExceptionHandler in WebApiConfig.cs like this:

    You can also use something like ValidationFilter to take care of invalid model states in an easy way (by replying with a 422). After you have all your desired/needed exception handlers in place, it’s time to setup the client side. You can easily intercept every HTTP request and act upon responses from the server with an http request interceptor that looks like this:

    Now you have a centralized way to handle all kinds of WebAPI errors. Simply build upon this infrastructure and add whatever else you might need. In one of the future posts I’ll explain how to create basic html input element directives (for text, textarea, number, datetime and similar fields) which also seamlessly take care of modelState error messages by presenting them to the user just below the input element.

  • Handling .net’s model state with ValidationFilter attribute

    Handling .net’s model state with ValidationFilter attribute

    This is a simple ValidationFilter class which you can attach to any .net controller action method:

    What it provides is a “one-line” way to handle invalid model states. For example – instead of doing ModelState.IsValid check on each POST/PUT action, and then handling it manually again and again (by basically copying and pasting the same chunk of code all around your controllers) you can simply reuse the [ValidationFilter] attribute in front of your action and it will check whether the ModelState is valid. If it is, the action method will execute whatever code it has inside of it, and if it’s not the WebAPI will respond with a 422 (Unprocessable Entity) together with the associated ModelState error messages.

    For example if you had this model/action combination:

    …and you didn’t send any data to the WebAPI in your POST request, this is the 422 response that you would get back:

    This response can then be further parsed/handled by the client-side without problems.

  • KatKiss >> Cyanogenmod (Asus TF300T)

    KatKiss >> Cyanogenmod (Asus TF300T)

    Ever since my little TF300T rooting/flashing adventure I have had nothing but trouble with Cyanogenmod on my TF300T. I flashed 3 new versions throughout 3 months, but none of them were stable or fast enough. I have been a long time Cyanogenmod user and I like it a lot so that was my obvious choice. But the tablet would often become very, very slow, couldn’t connect to Wi-Fi and wasn’t even able to run certain apps. There are also other issues people reported and it seemed that things wouldn’t change in future.. I guess the device is simply not popular enough to have enough people caring about and maintaining it’s CM version. Lately I started thinking I would either have to flash an older version of Android (perhaps even Asus stock crap) or put the device to sleep in a dark corner of my drawer..

    Hopefully, I remembered seeing everyone giving praise to the KatKiss ROM so I decided to give it one last try and give it a go. Man was this a pleasant surprise. The device is now bursting with speed! It’s really responsive, I haven’t noticed any bugs so far, it looks nice and I have the latest Android 5.1 features. It’s even faster than what it was with the old 4.x stock Android, it can now run anything (including Hearthstone! :D). I’m really happy as this means I will now be able to use the device with joy again.

    If you’re looking for a fresh rom for your TF300T – avoid CM and give KatKiss a try, it will save you time.

    Mad props to timduru! :)

  • Codealike bug squashing

    Codealike bug squashing

    I stumbled upon codealike a bit more than a month ago and decided to give it a try. I was interested in programming language usage percentages and coding vs. debugging vs. building statistics as well as some additional stuff that they track. After a while I even decided to get a premium for a month to see what the differences are and whether it would be worth for me personally to pay the fee.

    Today I discovered a potential bug in their system that I thought could probably lose them some money so I submitted a bug ticket. In 10 minutes, I got a response from their team. After that, we exchanged a few emails they were happy that I notified tham and in the end they even awarded me with one year of VIP subscription. Which is even better as it means I can now use their staging environment to experience the latest features they’re working on. That was quite cool of them and I guess it pays off to be a good guy. :)

  • Diving into Gulp

    Diving into Gulp

    For some time now I’ve been waiting to learn how to Gulp properly. As I started working on a home pet-project to keep sharp and get more in-depth with certain things, this was a good opportunity to learn more about Gulp as well. So far, I have mostly been using either famous-ish premade gulp scripts or the tools that came built-in (or as an extension) with Visual Studio. But that never really felt right, more like a cheap get-away.

    Anyway, I went through JavaScript Build Automation With Gulp.js. It’s a Pluralsight ~5hr course by John Papa who is quite a famous name within Angular circles for his angular-styleguide. I loved every bit of the course. It’s easy to follow, goes through the whole setup, explains the how’s and why’s and gives you code samples. I highly recommend it as it will give you a very solid gulp foundation and will make you more productive.

    Right now I’m in the middle of bootstrapping his gulp-patterns, and I’m taking out the stuff that I don’t need. Great base, and I feel very happy about the whole thing. :)

  • Invalidating Angular template cache with .net BundleConfig.cs

    Invalidating Angular template cache with .net BundleConfig.cs

    You most probably had the problem of old Angular templates not clearing themselves up from the browser cache in production which can cause various problems. If you’re using gulp, you can use something like gulp-angular-templatecache to tackle this problem (which would also be a preferred way of doing this) but if you’re using .net’s BundleConfig.cs to bundle your scripts, you’ll have to add a bit of code to your app.

    Say your app bundle section inside BundleConfig.cs looks something like this:

    Then you would include that inside your index.cshtml (or some other Razor template) like this:

    And this is how the generated HTML script include tag would look in production:

    <script src=”/bundles/my-app?v=6XgTJerMdBNglpDE1l_sgb9JgKJDXS35tee8zasXvLk1″></script>

    What will change upon every deploy is the “v” (version) hash at the end of bundle link. We can leverage that to keep track of the current version by extracting and storing that value within a cookie. Then on a full page reload (F5) if the version hash changed – we can simply clear the template cache and let the user continue using the app. The app will then have to re-cache all the templates once they are requested by the router or a directive.

    That piece of logic can be stored inside app.js on MyApp.run(). Here is how I did it:

    NOTE: I use lodash in every project so if you’re not using it, you might have to write your own code to replace _.find() and _.contains()

Back to top