As the follow up reply above suggests, you need to start by separating software from hardware issues. You can probably find out a lot more information by looking at the system log i.e. run a logcat to get more verbose details about the charging error. Depending on what you find there, you could proceed to steps such as resetting the battery stats or the suggestions that follow in this post.
If you aren't willing to do a logcat then the first thing that you should try is booting to recovery mode and wipe the system cache That may well resolve your issue right there.
If not, then heed the advice in the previous post. Backup anything important to you, then flash a 100% stock ROM, followed by a factory data reset. If that resolves the issue then the cause was almost certainly corrupt system files or settings.
If the problem still remains, then you have to start looking at hardware causes, starting with trying a different cable, different battery, gently rocking the cable in the USB socket at the base of the phone to try and discern if the pins are bent or making irregular contact, etc.
Whether the problem would be repaired free of charge under warranty is uncertain as we don't know yet if the cause is an unexpected failure or something that might be attributed to abuse e.g. dropped phone or damage to the USB port from rough use.