A drug peddler caught selling heroin and crack cocaine from his car has avoided jail “by a whisker”.