{"record":{"id":"3eff94c0e2249b07","repo":"zxing/zxing","slug":"failed-to-encode-input","errorCode":null,"errorMessage":"Failed to encode \\\"\" + input + \"\\\"","messagePattern":"Failed to encode \\\\\"\" \\+ input \\+ \"\\\\\"","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/google/zxing/datamatrix/encoder/MinimalEncoder.java","lineNumber":475,"sourceCode":"      }\n    }\n\n    int minimalJ = -1;\n    int minimalSize = Integer.MAX_VALUE;\n    for (int j = 0; j < 6; j++) {\n      if (edges[inputLength][j] != null) {\n        Edge edge = edges[inputLength][j];\n        int size = j >= 1 && j <= 3 ? edge.cachedTotalSize + 1 : edge.cachedTotalSize; //C40, TEXT and X12 need an\n                                                                                       // extra unlatch at the end\n        if (size < minimalSize) {\n          minimalSize = size;\n          minimalJ = j;\n        }\n      }\n    }\n\n    if (minimalJ < 0) {\n      throw new IllegalStateException(\"Failed to encode \\\"\" + input + \"\\\"\");\n    }\n    return new Result(edges[inputLength][minimalJ]);\n  }\n\n  private static final class Edge {\n    private static final int[] allCodewordCapacities = {3, 5, 8, 10, 12, 16, 18, 22, 30, 32, 36, 44, 49, 62, 86, 114,\n                                                        144, 174, 204, 280, 368, 456, 576, 696, 816, 1050, 1304, 1558};\n    private static final int[] squareCodewordCapacities = {3, 5, 8, 12, 18, 22, 30, 36, 44, 62, 86, 114, 144, 174, 204,\n                                                           280, 368, 456, 576, 696, 816, 1050, 1304, 1558};\n    private static final int[] rectangularCodewordCapacities = {5, 10, 16, 33, 32, 49};\n    private final Input input;\n    private final Mode mode; //the mode at the start of this edge.\n    private final int fromPosition;\n    private final int characterLength;\n    private final Edge previous;\n    private final int cachedTotalSize;\n\n    private Edge(Input input, Mode mode, int fromPosition, int characterLength, Edge previous) {","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/zxing/zxing/blob/19aa2d8254410e161f04dc3c928e68d5e90233c2/core/src/main/java/com/google/zxing/datamatrix/encoder/MinimalEncoder.java#L457-L493","documentation":"Thrown by MinimalEncoder.encodeHighLevel(...) when the minimum-cost path search over Data Matrix encoding modes finds no complete path (minimalJ stays -1). MinimalEncoder is a dynamic-programming encoder that, like MinimalECIInput, tries every mode combination; if no mode sequence can represent the input it gives up with this IllegalStateException. Under normal use the upstream ISO-8859-1 guard (EncoderContext) rejects unencodable input first, so this is a backstop.","triggerScenarios":"Passing input to MinimalEncoder that no combination of Data Matrix modes can represent. In practice reached only via direct use of MinimalEncoder bypassing the normal EncoderContext charset guard.","commonSituations":"Custom Data Matrix pipelines that call MinimalEncoder directly with bytes/chars outside the representable range, or after manipulating internal encoding state. Rare from the public DataMatrixWriter.","solutions":["Route input through EncoderContext / the standard DataMatrixWriter so the Latin-1 check rejects bad input early with a clearer message.","Ensure the input string is ISO-8859-1 representable before passing to MinimalEncoder.","Log the exact failing input to identify the unencodable character."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Route through EncoderContext/DataMatrixWriter so the Latin-1 check rejects bad input early;\n// confirm input is ISO-8859-1 before any direct MinimalEncoder use.","typeGuard":"static boolean isLatin1(String s) {\n  return new String(s.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1).equals(s);\n}","tryCatchPattern":"try {\n  MinimalEncoder.encodeHighLevel(input);\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Failed to encode\")) {\n    // surface clearer 'unsupported characters' error to caller\n  }\n  throw e;\n}","preventionTips":["Use the public DataMatrixWriter rather than MinimalEncoder directly.","Validate ISO-8859-1 coverage before encoding.","Log the failing input to identify the unencodable character."],"tags":["zxing","datamatrix","minimal-encoder","encoding"],"backgroundTag":null,"analyzedSha":"19aa2d8254410e161f04dc3c928e68d5e90233c2","analyzedAt":"2026-08-14T03:07:53.428Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}