How to use debrief - 10 common examples

To help you get started, we’ve selected a few debrief examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nvie / decoders / src / array.js View on Github external
clone.splice(
                index,
                1,
                annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
            );

            // const errValue = [];
            // if (index > 0) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            // errValue.push(
            // );
            // if (index < iterable.length - 1) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            return Err(annotate(clone));
        }
        index++;
    }
    return Ok(results);
}
github nvie / decoders / src / array.js View on Github external
function all(iterable: Array>, blobs: Array): DecodeResult> {
    const results: Array = [];
    let index = 0;
    for (const result of iterable) {
        try {
            const value = result.unwrap();
            results.push(value);
        } catch (ann) {
            // Rewrite the annotation to include the index information, and inject it into the original blob
            const clone = [...blobs];
            clone.splice(
                index,
                1,
                annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
            );

            // const errValue = [];
            // if (index > 0) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            // errValue.push(
            // );
            // if (index < iterable.length - 1) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            return Err(annotate(clone));
        }
        index++;
    }
    return Ok(results);
github nvie / decoders / src / array.js View on Github external
export const poja: Decoder> = (blob: mixed) => {
    if (!Array.isArray(blob)) {
        return Err(annotate(blob, 'Must be an array'));
    }
    return Ok(
        // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray
        // instead of Array.  For rationale, see
        // https://github.com/facebook/flow/issues/7684.  In this case, we
        // don't want to output read-only types because it's up to the user of
        // decoders to determine what they want to do with the decoded output.
        // If they want to write items into the array, that's fine!
        // The fastest way to turn a read-only array into a normal array in
        // Javascript is to use .slice() on it, see this benchmark:
        // http://jsben.ch/lO6C5
        blob.slice()
    );
};
github nvie / decoders / src / object.js View on Github external
}
        }

        // Deal with errors now. There are two classes of errors we want to
        // report.  First of all, we want to report any inline errors in this
        // object.  Lastly, any fields that are missing should be annotated on
        // the outer object itself.
        const fieldsWithErrors = Object.keys(fieldErrors);
        if (fieldsWithErrors.length > 0 || missing.size > 0) {
            let err;

            if (fieldsWithErrors.length > 0) {
                const errorlist = fieldsWithErrors.map(k => [k, fieldErrors[k]]);
                err = annotateFields(blob, errorlist);
            } else {
                err = annotate(blob);
            }

            if (missing.size > 0) {
                const errMsg = Array.from(missing)
                    .map(key => `"${key}"`)
                    .join(', ');
                const pluralized = missing.size > 1 ? 'keys' : 'key';
                err = annotate(err, `Missing ${pluralized}: ${errMsg}`);
            }

            return Err(err);
        }

        return Ok(record);
    });
}
github nvie / decoders / src / object.js View on Github external
fieldErrors[key] = ann;
                }
            }
        }

        // Deal with errors now. There are two classes of errors we want to
        // report.  First of all, we want to report any inline errors in this
        // object.  Lastly, any fields that are missing should be annotated on
        // the outer object itself.
        const fieldsWithErrors = Object.keys(fieldErrors);
        if (fieldsWithErrors.length > 0 || missing.size > 0) {
            let err;

            if (fieldsWithErrors.length > 0) {
                const errorlist = fieldsWithErrors.map(k => [k, fieldErrors[k]]);
                err = annotateFields(blob, errorlist);
            } else {
                err = annotate(blob);
            }

            if (missing.size > 0) {
                const errMsg = Array.from(missing)
                    .map(key => `"${key}"`)
                    .join(', ');
                const pluralized = missing.size > 1 ? 'keys' : 'key';
                err = annotate(err, `Missing ${pluralized}: ${errMsg}`);
            }

            return Err(err);
        }

        return Ok(record);
github nvie / decoders / src / mapping.js View on Github external
}
                } catch (e) {
                    /* istanbul ignore else */
                    if (isAnnotation(e)) {
                        tuples.length = 0; // Clear the tuples array
                        errors.push([key, e]);
                    } else {
                        // Otherwise, simply rethrow it
                        /* istanbul ignore next */
                        throw e;
                    }
                }
            });

            if (errors.length > 0) {
                return Err(annotateFields(blob, errors));
            } else {
                return Ok(new Map(tuples));
            }
        }
    );
github nvie / decoders / src / mapping.js View on Github external
Object.keys(blob).forEach((key: string) => {
                const value: T = blob[key];
                const result = decoder(value);
                try {
                    const okValue = result.unwrap();
                    if (errors.length === 0) {
                        tuples.push([key, okValue]);
                    }
                } catch (e) {
                    /* istanbul ignore else */
                    if (isAnnotation(e)) {
                        tuples.length = 0; // Clear the tuples array
                        errors.push([key, e]);
                    } else {
                        // Otherwise, simply rethrow it
                        /* istanbul ignore next */
                        throw e;
                    }
                }
            });
github nvie / decoders / src / object.js View on Github external
// NOTE: We're using .keys() here over .entries(), since .entries()
        // will type the value part as "mixed"
        for (const key of Object.keys(mapping)) {
            const decoder = mapping[key];
            const value = blob[key];
            const result = decoder(value);
            try {
                record[key] = result.unwrap();

                // If this succeeded, remove the key from the missing keys
                // tracker
                missing.delete(key);
            } catch (ann) {
                /* istanbul ignore next */
                if (!isAnnotation(ann)) {
                    throw ann;
                }

                // Keep track of the annotation, but don't return just yet. We
                // want to collect more error information.
                if (value === undefined) {
                    // Explicitly add it to the missing set if the value is
                    // undefined.  This covers explicit undefineds to be
                    // treated the same as implicit undefineds (aka missing
                    // keys).
                    missing.add(key);
                } else {
                    fieldErrors[key] = ann;
                }
            }
        }
github nvie / decoders / src / date.js View on Github external
export const date: Decoder = (value: mixed) =>
    isDate(value) ? Ok(((value: cast): Date)) : Err(annotate(value, 'Must be a Date'));
github nvie / decoders / src / constants.js View on Github external
export const undefined_: Decoder = (blob: mixed) =>
    blob === undefined ? Ok(blob) : Err(annotate(blob, 'Must be undefined'));

debrief

Object serialization and annotation, for use in human-friendly error messages

MIT
Latest version published 4 years ago

Package Health Score

45 / 100
Full package analysis