{"record":{"id":"c3d5f01aa87050d5","repo":"donnemartin/interactive-coding-challenges","slug":"license-key-must-not-be-empty","errorCode":null,"errorMessage":"license_key must not be empty","messagePattern":"license_key must not be empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"online_judges/license_key/format_license_key_solution.ipynb","lineNumber":122,"sourceCode":"   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def format_license_key(self, license_key, k):\\n\",\n    \"        if license_key is None:\\n\",\n    \"            raise TypeError('license_key must be a str')\\n\",\n    \"        if not license_key:\\n\",\n    \"            raise ValueError('license_key must not be empty')\\n\",\n    \"        formatted_license_key = []\\n\",\n    \"        num_chars = 0\\n\",\n    \"        for char in license_key[::-1]:\\n\",\n    \"            if char == '-':\\n\",\n    \"                continue\\n\",\n    \"            num_chars += 1\\n\",\n    \"            formatted_license_key.append(char.upper())\\n\",\n    \"            if num_chars >= k:\\n\",\n    \"                formatted_license_key.append('-')\\n\",\n    \"                num_chars = 0\\n\",\n    \"        if formatted_license_key and formatted_license_key[-1] == '-':\\n\",\n    \"            formatted_license_key.pop(-1)\\n\",\n    \"        return ''.join(formatted_license_key[::-1])\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/license_key/format_license_key_solution.ipynb#L104-L140","documentation":"Raised by Solution.format_license_key when license_key is an empty (or falsy) string. Formatting an empty key is meaningless, so after the None check the method rejects empty input with ValueError before attempting to build the formatted result.","triggerScenarios":"Calling format_license_key('', 4) or with any falsy string — typically from an empty form field, blank config value, or a stripped input that became ''.","commonSituations":"Web form handlers passing raw user input; config values present but blank; tests covering the empty-string edge case.","solutions":["Validate non-empty input before calling: if not key: raise/return early","Trim and require content: key = key.strip(); require key","Provide a default key when the input source may be blank"],"exampleFix":"# before\nkey = request.args.get('key', '')\nsolution.format_license_key(key, 4)\n\n# after\nkey = request.args.get('key', '').strip()\nif not key:\n    return 'key is required', 400\nsolution.format_license_key(key, 4)","handlingStrategy":"validation","validationCode":"license_key = license_key.strip() if license_key else ''\nif not license_key: raise ValueError('license_key is required')\nsolution.format_license_key(license_key, k)","typeGuard":"def is_non_empty_str(s): return isinstance(s, str) and bool(s.strip())","tryCatchPattern":"try:\n    key = solution.format_license_key(raw, k)\nexcept ValueError:\n    return 'license key required', 400","preventionTips":["Strip and check form/config strings before use","Reject blank inputs at the UI/API edge, not in algorithms"],"tags":["python","input-validation","empty-string","strings"],"backgroundTag":"empty-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}